Jquery-ajax-async

来源:互联网 发布:mac的numbers使用教程 编辑:程序博客网 时间:2024/05/22 07:44

jQuery同步问题:

google chrome浏览器给出的结果是:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check .

jQuery 官网给出的结果是:

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

原文地址:

http://api.jquery.com/jQuery.ajax/

参考网址:

http://xhr.spec.whatwg.org/.


一下转摘的一篇关于‘jQuery jqXHR对象的属性和方法’的文章

原文地址:http://www.365mini.com/page/jquery-jqxhr-object.htm

在 jQuery 1.4 之前(包括1.4),$.ajax()方法返回的是浏览器原生的XMLHttpRequest对象。

从 jQuery 1.5 开始,$.ajax()方法返回jQuery自己的XMLHttpRequest对象(一般简称jqXHR)。之所以这样做,是因为 jQuery 1.5 引入了延迟对象jQuery.Deferred,以便于更好地处理和执行回调函数。

你可以简单地理解为jqXHR对象是jQuery自己伪造的一个XMLHttpRequest对象和$.Deferred对象的结合体。

jqXHR是浏览器原生XMLHttpRequest对象的超集。例如,它也包含responseTextresponseXML属性,以及getResponseHeader()方法。当传输机制与 XMLHttpRequest 对象不同时(例如,用一个<script>标签来完成JSONP请求),jqXHR对象会尽可能地模拟本地 XMLHttpRequest 对象的功能。

从jQuery 1.5.1开始,jqXHR对象也overrideMimeType()方法(它在 1.4.x 中也可用,但在 1.5 中被临时移除)。overrideMimeType()方法可用于beforeSend()的回调函数中,比如用来修改 Content-Type 响应头。

从jQuery 1.5开始,$.ajax()返回的jqXHR对象实现了Promise接口,包括其所有的属性、方法和行为。因此,我们通过jqXHR可以非常简单地为本次$.ajax()绑定AJAX请求对应状态执行的回调函数。

我们通过解剖一个完整的jqXHR对象,来了解jqXHR对象的属性和方法:

var jqXHR = {    abort: function (statusText){        // 取消请求,关闭连接                    },    always: function (){        // 设置请求完成(无论成功或失败)时需要执行的一个或多个回调函数    },    complete: function (){        // always()函数的别名,设置请求完成(无论成功或失败)时需要执行的一个或多个回调函数    },    done: function (){        // 设置请求成功时需要执行的一个或多个回调函数    },    error: function (){        // fail()函数的别名,设置请求失败时需要执行的一个或多个回调函数    },    fail: function (){        // 设置请求失败时需要执行的一个或多个回调函数                    },    getAllResponseHeaders: function (){        // 获取响应头信息的原始字符串            },    getResponseHeader: function (key){        // 获取响应头中指定名称的值         },    overrideMimeType: function (type){        // 重写 Content-Type响应头           },    pipe: function (){        // then()函数的别名,分别设置请求成功、失败、正在进行时需要执行的回调函数               },    progress: function (){        // 设置Deferred对象生成进度通知时需要执行的回调函数    },    promise: function (obj){        // 为指定对象追加Promise对象的成员,以充当Promise对象     },    readyState: 4, // 请求的状态    responseText: "<!DOCTYPE html><html>......</html>", // 响应的文本内容    setRequestHeader: function (name, value){        // 设置请求头    },    state: function (){        // 确定一个Deferred对象的当前状态,例如:"pending"、"resolved"、"rejected"                   },    status: 200,    statusCode: function (map){             // 设置响应指定状态码需要执行的回调函数        // map形如:{ 301:function(){}, 404:function(){} }         },    statusText: "OK", // 状态响应头中的描述文本    success: function (){        // done()函数的别名,设置请求成功时需要执行的一个或多个回调函数    },    then: function (){        // 分别设置请求成功、失败、正在进行时需要执行的回调函数                   }};

0 0
原创粉丝点击