1. jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求
1.1 参数
url(String) : 发送请求的URL地址.
data(Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
callback(Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)
1.2 jQuery的Ajax与Java通过POST方式交互
1.2.1 html代码
Ajax获取数据
1.2.2 style代码
#main {
margin: 0 auto;
width: 400px;
}
#container {
width: 400px;
height: 300px;
border: 1px dashed #666;
text-align: center;
line-height: 300px;
}
1.2.3 Javsscript代码
$(function() {
$('#myBut').click(function() {
$.post("JqueryAjaxServlet", {
age : 18,
name : "zhang"
}, function(data, textStatus) {
var container = $('#container');
var resultData = $.parseJSON(data);
var age = resultData.age;
var name = resultData.name;
container.html("name:" + name + "," + "age:" + age);
});
});
});
1.2.4 JqueryAjaxServlet.java代码
public class JqueryAjaxServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String age = request.getParameter("age");
String name = request.getParameter("name");
String personJSON = "{\"name" + "\":\"" + name + "\"," + "\"age"
+ "\":" + age + "}";
System.out.println(personJSON);
response.getWriter().write(personJSON);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}1.2.5 web.xml代码
JqueryAjaxServlet
com.jquery.ajax.com.JqueryAjaxServlet
JqueryAjaxServlet
/JqueryAjaxServlet
index.jsp
原文:http://blog.csdn.net/zbw18297786698/article/details/51334103