javascript ajax的语法

ajax参数:

详细参数转到如下地址:

http://www.w3school.com.cn/jquery/ajax_ajax.asp

 

$.ajax语法:

jQuery.ajax([settings])

http://www.w3school.com.cn/jquery/ajax_ajax.asp

$.post语法:

jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)

$.ajax({

type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

 

注意:

 

data:有两种方式:

  1.一种是普通url传参

    例:data:name=张三&age=12

  2.用json数组:

    例:data:{name:"张三",age:12}

  这两者的区别:(第一种缺点)

    如果参数里有‘&’那么就麻烦了 ,接收不到或者不完整。

    比如:data:"name=张三&abc&age=12"    就会出错;

  解决方法:

    encodeURIComponent();   进行转义;

 

 

  http://www.w3school.com.cn/jquery/ajax_post.asp

get语法:

$(selector).get(url,data,success(response,status,xhr),dataType)

$.ajax({

type: 'GET', (可省,默认是get)
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

 参数:

type:默认值,get

url:请求地址(必须)

data:数据(可选)

success:请求成功回调函数(可选)

    函数的参数:(可选)

    response - 包含来自请求的结果数据

    status - 包含请求的状态

    xhr - 包含 XMLHttpRequest 对象

dataType:服务器响应的数据类型(可选)

  dataType的类型:

默认执行智能判断(xml、json、script 或 html)。

 

posted @ 2014-04-18 08:10  土豆哥  阅读(593)  评论(0编辑  收藏  举报