最近要使用到jquery,整理下jquery中异步的格式:
[color=red][size=large]1)客户端脚本[/size][/color]
上面的例子是最简单和常用的ajax的请求方式。
data类型的格式一般常用的就是json的方式进行传递
[color=red][size=large]2)在服务器端的处理[/size][/color]
[color=red][size=large]3)客户端接收服务器传递过来的数据[/size][/color]
[color=red][size=large]1)客户端脚本[/size][/color]
$.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},
[color=red][size=large]2)在服务器端的处理[/size][/color]
// 获得数据
request.getParameter("requestType");
// .......自己的逻辑处理
// 将数据返回到客户端 json类型的数据
JSONObject json = new JSONObject();
json.put("success", isSuccess);
//.........添加多个要返回的数据
response.getWriter().write(json.toString());
response.getWriter().flush();
[color=red][size=large]3)客户端接收服务器传递过来的数据[/size][/color]
// ajax把传递回来的数据都保存在了
success: function(msg)
{}
// 可以把msg直接看成一个js对象进行操作
success: function(msg)
{
alert(msg);
alert(msg.success);
}