ajax
jQuery在全局对象jQuery(也就是$)绑定了ajax()函数,可以处理ajax请求。ajax(url,settings)函数需要接收一个url和一个可选的settings对象,常用选项如下:
- async:是否异步执行ajax请求,默认为true,千万不要指定为false
- method:发送的method,省缺为‘get’,可指定为‘post’,‘put’等
- contentType:发送的post请求的格式,默认为‘application/x-www-form-urlencoded;charset=uft-8’,也可以指定为‘text/plain’、‘application/json’
- data:发送的数据,可以是字符串、数组或object。如果是get请求,data将被转换成query附加到url上,如果是post请求,根据contentType把data序列化成合适的格式
- headers:发送的额外的http头,必须为object
- dataType:接收的数据格式,可以指定为‘html’、'xml'、'json'、'text'等,省缺情况下根据相应的Content-Type猜测
get

第二个参数如果是object,jQuery自动把它变成query string然后加到url后面,实际的url是:

post
post()和get()类似,但是出入的第二个参数默认被序列化为application/x-www-form-urlencoded:

实际构造的数据:name=Bob%20Lee&check=1作为POST的body被发送
getJSON
由于JSON用得越来越普遍,所以jQuery也提供了 getJSON() 方法来快速通过
GET获取一个JSON对象:

网友评论