ITeye博客

jquery 的ajax 基本用法

编程足球 · 2011-07-13 01:11 · JavaScript · jquery · Web前端 · Markdown

最近要使用到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);
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。