---
title: "jquery 的ajax 基本用法"
author: "编程足球"
date: "2011-07-13T01:11:12Z"
tags: []
categories: ["Web前端"]
slug: "jquery-ajax"
draft: false
lastmod: "2011-07-13T01:11:12Z"
id: 1122966
url: "https://lingf.iteye.com/blog/1122966"
---

最近要使用到jquery,整理下jquery中异步的格式：

<span style="color:red">1)客户端脚本</span>

```java

$.ajax({
     type: "POST",// 请求类型
     url: "selectDocument.do",//请求的地址
     dataType : "json",//预期服务器返回的数据类型
     data: {},//  发送到服务器端的数据
     success: function(msg)
     {
//      成功时候调用
     },
     error : function()
     {
//      出错,时候调用
      }
});

```


上面的例子是最简单和常用的ajax的请求方式。
data类型的格式一般常用的就是json的方式进行传递

```java

var propertyList = 'xxxxx';
data: {requestType : 'documentwholeExist',property:propertyList},

```


<span style="color:red">2)在服务器端的处理</span>

```java

//  获得数据
request.getParameter("requestType");
//  .......自己的逻辑处理


//   将数据返回到客户端 json类型的数据
JSONObject json = new JSONObject();
json.put("success", isSuccess);
//.........添加多个要返回的数据
response.getWriter().write(json.toString());
response.getWriter().flush();

```


<span style="color:red">3)客户端接收服务器传递过来的数据</span>

```java

//   ajax把传递回来的数据都保存在了
success: function(msg)
{}

//   可以把msg直接看成一个js对象进行操作
success: function(msg)
{
     alert(msg);
     alert(msg.success);
}


```