jquery之ajax之$.get方法的使用

jquery对ajax进行了封装,非常方便。

自己用$.get()方法写了个小demo,包括客户端和服务端。

客户端:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="div">
        <div >
            <span class="f1">电影</span>
            <span class="f2"><input id="btn" type="button" value="加载" ></span>
        </div>    
        <ul></ul>
    </div>
    <script type="text/javascript">
        $(function(){
            $("#btn").bind("click",function(){
                var $this= $(this);
                $.get("../jsp/test4.jsp",function(data){
                    $this.attr("disabled","true");
                    $("ul").append("<li>"+ data+ "</li>");
                });
            });
        });
    </script>
</body>
</html>

服务器端(jsp):

<%@page import="org.json.simple.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String msg= "你好吗?ajax";
        response.getWriter().print(msg);
    %>
</body>
</html>

 如果需要在服务器端返回json格式的数据,可以使用json_simple-1.1.jar类库中的JSONObject.

/* JSONObject jsonObject= new JSONObject();
jsonObject.put("name", "小强");
jsonObject.put("say", "你真牛");
/* out.print(jsonObject.toJSONString());
out.flush(); */
/* response.getWriter().print(jsonObject.toJSONString());
System.out.print("visit jsp success2"); */
//System.out.print(jsonObject.toJSONString());

原文地址:https://www.cnblogs.com/westward/p/5746018.html