`
编程足球
  • 浏览: 251101 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

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);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics