Heim  >  Artikel  >  Web-Frontend  >  jquery1.4 教程二 ajax方法的改进_jquery

jquery1.4 教程二 ajax方法的改进_jquery

WBOY
WBOYOriginal
2016-05-16 18:33:46895Durchsuche
1 允许序列化嵌套的参数。
比如:{foo: ["bar", "baz"]} 将被序列化成foo[]=bar&foo[]=baz,而不是序列化成以前的foo=bar&foo=baz。

如果你想要使用旧的序列化方式,有三种设置方法:
复制代码 代码如下:

jQuery.ajaxSettings.traditional = true;
jQuery.param( stuff, true );
$.ajax({ data: stuff, traditional: true });

2 自动检测json和javascript的数据类型。
1.4后返回json或javascript可以不用设置dataType 了,将会根据application/json或application/x-javascript自动判断类型。但后台返回的数据类型务必指定。

3支持添加html头信息。
$.ajax()新增一个属性ifModified: true ,设置为true,可以有效利用浏览器缓存(目前我还有试过其效果。)

4使用原生的JSON.parse,来解析json。
1.4会对ajax返回回来的json进行合法性验证,错误的json格式将不予解析,比如{foo: "bar"}。

5 $.serialize()序列化表单时,现在可以序列化html5的表单元素。
……相当的与时俱进,html5我还没认真看过…..

6 留意新增一个全新属性Context,非常有用处
Context的出现真是太及时了,Context会简化你的ajax请求。Context的作用是你可以自定义上下文,也就是指定ajax中回调函数的this。来看代码:
复制代码 代码如下:

jQuery.ajax({
url: "test.html",
context: document.body,
success: function(){
jQuery(this).addClass("done");
}
});

代码success回调函数中的this将指向document.body

7 显式设置content-type
在1.4以前,如果你没有设置参数data,$.ajax()会忽略contentType 这个参数的值,1.4contentType在ajax请求时都要发送。

8 你可以指定JSONP的回调函数名
9 默认不允许跨域请求
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn