jquery 的ajax 基本用法
最近要使用到jquery,整理下jquery中异步的格式:1)客户端脚本
$.ajax({ type: "POST",// 请求类型 url: "selectDocument.do",//请求的地址 dataType : "json",//预期服务器返回的数据类型 data: {},//发送到服务器端的数据 success: function(msg) {// 成功时候调用 }, error : function() {// 出错,时候调用 }});
上面的例子是最简单和常用的ajax的请求方式。
data类型的格式一般常用的就是json的方式进行传递
var propertyList = 'xxxxx';data: {requestType : 'documentwholeExist',property:propertyList},
2)在服务器端的处理
//获得数据request.getParameter("requestType");//.......自己的逻辑处理// 将数据返回到客户端 json类型的数据JSONObject json = new JSONObject();json.put("success", isSuccess);//.........添加多个要返回的数据response.getWriter().write(json.toString());response.getWriter().flush();
3)客户端接收服务器传递过来的数据
// ajax把传递回来的数据都保存在了success: function(msg){}// 可以把msg直接看成一个js对象进行操作success: function(msg){ alert(msg); alert(msg.success);}
页:
[1]