`
zccst
  • 浏览: 3290298 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

$.ajax的error,complete,success方法

阅读更多
作者:zccst

2015-03-30
今天发现从1.8后,jQuery做了调整

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information). These methods take one or more function arguments that are called when the $.ajax() request terminates. This allows you to assign multiple callbacks on a single request, and even to assign callbacks after the request may have completed. (If the request is already complete, the callback is fired immediately.) Available Promise methods of the jqXHR object include:

jqXHR.done(function( data, textStatus, jqXHR ) {});
An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details.

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { });
An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {});
Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details.

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
 
// Perform other work here ...
 
// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second complete" );
});

The this reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves.

For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:

readyState
status
statusText
responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
setRequestHeader(name, value) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
getAllResponseHeaders()
getResponseHeader()
statusCode()
abort()
No onreadystatechange mechanism is provided, however, since done, fail, always, and statusCode cover all conceivable requirements.

Callback Function Queues

The beforeSend, error, dataFilter, success and complete options all accept callback functions that are invoked at the appropriate times.

As of jQuery 1.5, the fail and done, and, as of jQuery 1.6, always callback hooks are first-in, first-out managed queues, allowing for more than one callback for each hook. See Deferred object methods, which are implemented internally for these $.ajax() callback hooks.

The callback hooks provided by $.ajax() are as follows:

beforeSend callback option is invoked; it receives the jqXHR object and the settings object as parameters.
error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".
dataFilter callback option is invoked immediately upon successful receipt of response data. It receives the returned data and the value of dataType, and must return the (possibly altered) data to pass on to success.
success callback option is invoked, if the request succeeds. It receives the returned data, a string containing the success code, and the jqXHR object.
Promise callbacks — .done(), .fail(), .always(), and .then() — are invoked, in the order they are registered.
complete callback option fires, when the request finishes, whether in failure or success. It receives the jqXHR object, as well as a string containing the success or error code.



2015-03-27
官方文档写的很详细:http://api.jquery.com/jquery.ajax/
complete: function (XMLHttpRequest, textStatus) {
    //textStatus的值:success,notmodified,nocontent,error,timeout,abort,parsererror
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
    //textStatus的值:null, timeout, error, abort, parsererror
    //errorThrown的值:收到http出错文本,如 Not Found 或 Internal Server Error.
}


注意:complete和error时的textStatus不一样。




2014-12-16

ajax的参数,还需要仔细了解,包括每一个函数及函数参数

之前一直关注success方法,很少关注error方法,今天重点看一下
error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest.status);    //http响应状态
    alert(XMLHttpRequest.readyState);//5个状态
    alert(textStatus);               //六个值
},



请求失败时调用事件
默 认: 自动判断 (xml 或 html)。
error:function (XMLHttpRequest, textStatus, errorThrown)
{
}

参数有以下三个:XMLHttpRequest 对象、错误信息、(可选)捕获的错误对象。

一、第二个参数textStatus
如果发生了错误,错误信息(第二个参数)除了得到null之外,还可能是"timeout", "error", "notmodified" 和 "parsererror"。

textStatus: "timeout", "error", "notmodified" 和 "parsererror"。
(0)null
(1)timeout 超时
(2)error
(3)notmodified 未修改
(4)parsererror 解析错误

data:"{}", data为空也一定要传"{}";不然返回的是xml格式的。并提示parsererror.
parsererror的异常和Header 类型也有关系。及编码header('Content-type: text/html; charset=utf8');





二、第一个参数XMLHttpRequest

(1)XMLHttpRequest.readyState:

状态码

0 - (未初始化)还没有调用send()方法
1 - (载入)已调用send()方法,正在发送请求
2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
3 - (交互)正在解析响应内容
4 - (完成)响应内容解析完成,可以在客户端调用了



(2)XMLHttpRequest.status:
1xx-信息提示 
这些状态代码表示临时的响应。客户端在收到常规响应之前,应准备接收一个或多个1xx响应。 
100-继续。 
101-切换协议。 
2xx-成功 
这类状态代码表明服务器成功地接受了客户端请求。 
200-确定。客户端请求已成功。 
201-已创建。 
202-已接受。 
203-非权威性信息。 
204-无内容。 
205-重置内容。 
206-部分内容。 
3xx-重定向 
客户端浏览器必须采取更多操作来实现请求。例如,浏览器可能不得不请求服务器上的不同的页面,或通过代理服务器重复该请求。 
301-对象已永久移走,即永久重定向。 
302-对象已临时移动。 
304-未修改。 
307-临时重定向。 
4xx-客户端错误 
发生错误,客户端似乎有问题。例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息。400-错误的请求。 
401-访问被拒绝。IIS定义了许多不同的401错误,它们指明更为具体的错误原因。这些具体的错误代码在浏览器中显示,但不在IIS日志中显示: 
401.1-登录失败。 
401.2-服务器配置导致登录失败。 
401.3-由于ACL对资源的限制而未获得授权。 
401.4-筛选器授权失败。 
401.5-ISAPI/CGI应用程序授权失败。 
401.7–访问被Web服务器上的URL授权策略拒绝。这个错误代码为IIS6.0所专用。 
403-禁止访问:IIS定义了许多不同的403错误,它们指明更为具体的错误原因: 
403.1-执行访问被禁止。 
403.2-读访问被禁止。 
403.3-写访问被禁止。 
403.4-要求SSL。 
403.5-要求SSL128。 
403.6-IP地址被拒绝。 
403.7-要求客户端证书。 
403.8-站点访问被拒绝。 
403.9-用户数过多。 
403.10-配置无效。 
403.11-密码更改。 
403.12-拒绝访问映射表。 
403.13-客户端证书被吊销。 
403.14-拒绝目录列表。 
403.15-超出客户端访问许可。 
403.16-客户端证书不受信任或无效。 
403.17-客户端证书已过期或尚未生效。 
403.18-在当前的应用程序池中不能执行所请求的URL。这个错误代码为IIS6.0所专用。 
403.19-不能为这个应用程序池中的客户端执行CGI。这个错误代码为IIS6.0所专用。 
403.20-Passport登录失败。这个错误代码为IIS6.0所专用。 
404-未找到。 
404.0-(无)–没有找到文件或目录。 
404.1-无法在所请求的端口上访问Web站点。 
404.2-Web服务扩展锁定策略阻止本请求。 
404.3-MIME映射策略阻止本请求。 
405-用来访问本页面的HTTP谓词不被允许(方法不被允许) 
406-客户端浏览器不接受所请求页面的MIME类型。 
407-要求进行代理身份验证。 
412-前提条件失败。 
413–请求实体太大。 
414-请求URI太长。 
415–不支持的媒体类型。 
416–所请求的范围无法满足。 
417–执行失败。 
423–锁定的错误。 
5xx-服务器错误 
服务器由于遇到错误而不能完成该请求。 
500-内部服务器错误。 
500.12-应用程序正忙于在Web服务器上重新启动。 
500.13-Web服务器太忙。 
500.15-不允许直接请求Global.asa。 
500.16–UNC授权凭据不正确。这个错误代码为IIS6.0所专用。 
500.18–URL授权存储不能打开。这个错误代码为IIS6.0所专用。 
500.100-内部ASP错误。 
501-页眉值指定了未实现的配置。 
502-Web服务器用作网关或代理服务器时收到了无效响应。 
502.1-CGI应用程序超时。 
502.2-CGI应用程序出错。application. 
503-服务不可用。这个错误代码为IIS6.0所专用。 
504-网关超时。 
505-HTTP版本不受支持。 
FTP 
1xx-肯定的初步答复 
这些状态代码指示一项操作已经成功开始,但客户端希望在继续操作新命令前得到另一个答复。 
110重新启动标记答复。 
120服务已就绪,在nnn分钟后开始。 
125数据连接已打开,正在开始传输。 
150文件状态正常,准备打开数据连接。 
2xx-肯定的完成答复 
一项操作已经成功完成。客户端可以执行新命令。200命令确定。 
202未执行命令,站点上的命令过多。 
211系统状态,或系统帮助答复。 
212目录状态。 
213文件状态。 
214帮助消息。 
215NAME系统类型,其中,NAME是AssignedNumbers文档中所列的正式系统名称。 
220服务就绪,可以执行新用户的请求。 
221服务关闭控制连接。如果适当,请注销。 
225数据连接打开,没有进行中的传输。 
226关闭数据连接。请求的文件操作已成功(例如,传输文件或放弃文件)。 
227进入被动模式(h1,h2,h3,h4,p1,p2)。 
230用户已登录,继续进行。 
250请求的文件操作正确,已完成。 
257已创建“PATHNAME”。 
3xx-肯定的中间答复 
该命令已成功,但服务器需要更多来自客户端的信息以完成对请求的处理。331用户名正确,需要密码。 
332需要登录帐户。 
350请求的文件操作正在等待进一步的信息。 
4xx-瞬态否定的完成答复 
该命令不成功,但错误是暂时的。如果客户端重试命令,可能会执行成功。421服务不可用,正在关闭控制连接。如果服务确定它必须关闭,将向任何命令发送这一应答。 
425无法打开数据连接。 
426Connectionclosed;transferaborted. 
450未执行请求的文件操作。文件不可用(例如,文件繁忙)。 
451请求的操作异常终止:正在处理本地错误。 
452未执行请求的操作。系统存储空间不够。 
5xx-永久性否定的完成答复 
该命令不成功,错误是永久性的。如果客户端重试命令,将再次出现同样的错误。500语法错误,命令无法识别。这可能包括诸如命令行太长之类的错误。 
501在参数中有语法错误。 
502未执行命令。 
503错误的命令序列。 
504未执行该参数的命令。 
530未登录。 
532存储文件需要帐户。 
550未执行请求的操作。文件不可用(例如,未找到文件,没有访问权限)。 
551请求的操作异常终止:未知的页面类型。 
552请求的文件操作异常终止:超出存储分配(对于当前目录或数据集)。 
553未执行请求的操作。不允许的文件名。 
常见的FTP状态代码及其原因 
150-FTP使用两个端口:21用于发送命令,20用于发送数据。状态代码150表示服务器准备在端口20上打开新连接,发送一些数据。 
226-命令在端口20上打开数据连接以执行操作,如传输文件。该操作成功完成,数据连接已关闭。 
230-客户端发送正确的密码后,显示该状态代码。它表示用户已成功登录。 
331-客户端发送用户名后,显示该状态代码。无论所提供的用户名是否为系统中的有效帐户,都将显示该状态代码。 
426-命令打开数据连接以执行操作,但该操作已被取消,数据连接已关闭。 
530-该状态代码表示用户无法登录,因为用户名和密码组合无效。如果使用某个用户帐户登录,可能键入错误的用户名或密码,也可能选择只允许匿名访问。如果使用匿名帐户登录,IIS的配置可能拒绝匿名访问。 
550-命令未被执行,因为指定的文件不可用。例如,要GET的文件并不存在,或试图将文件PUT到您没有写入权限的目录。


如果您觉得本文的内容对您的学习有所帮助,您可以微信:
分享到:
评论
1 楼 u012206458 2017-03-30  
       

相关推荐

    jquery $.ajax各个事件执行顺序

    1.ajaxStart(全局事件) 2.beforeSend 3.ajaxSend(全局事件) 4.success 5.ajaxSuccess(全局事件) 6.error 7.ajaxError (全局事件) 8.complete 9.ajaxComplete(全局事件) 10.ajaxStop(全局事件)

    JqueryGrid无刷新分页

    $.ajax({ type: p.method, url: p.url, data:param, success: function(msg){ $.AddData(msg,showbox,p); }, error: function(msg){$.ErrorAjax(showbox,p.errorMsg);}, beforeSend:function(){$.AddLoading...

    谈谈Jquery ajax中success和complete有哪些不同点

    $.ajax({ type: "post", url: url, dataType:'html', success: function(data) { }, complete: function(XMLHttpRequest, textStatus) { }, error: function(){} }); success : 当请求成功时调用的函数。这个...

    jQuery 1.6 API 中文版

    如果accepts设置需要修改,推荐在$.ajaxSetup()方法中做一次。 asyncBoolean 默认: true 默认设置下,所有请求均为异步请求(也就是说这是默认设置为true)。如果需要发送同步请求,请将此选项设置为 false。...

    ajax-utils:用于 jQuery ajax 的包装器

    $.ajax({ type: 'POST', // some parameters success: function () { // when success }, error: function () { // when error }, complete: function () { // always } }); AjaxUtils 使您能够编写简单...

    jquery-1.1.3 效率提高800%

    如果还需要设置error和success回调函数,则需要使用$.ajax。 实例 请求test.php页,忽略返回值. $.get("test.php");请求test.php页并发送附加数据(忽略返回值). $.get("test.php", { name: "John", ...

    jquery电子文档chm

    $.ajax() takes one argument, an object of key/value pairs, that are used to initialize and handle the request. See below for a full list of the key/values that can be used. As of jQuery 1.2, you can...

    jquery 异步请求数据的三种方式

    $.ajax({ type: "POST", contentType: "application/json;charset=utf-8", url: "WebServicetest.asmx/HelloWorld", data: "{username:'cccc'}", dataType: "json", success: function (data) { alert(data)...

    jQuery中Ajax全局事件引用方式及各个事件(全局/局部)执行顺序

    7.ajaxError (全局事件) 8.complete(局部事件) 9.ajaxComplete(全局事件) 10.ajaxStop(全局事件) 其中,全局事件可以在ajax相关方法外引用(比如,通过该方式将ajax执行各个阶段的信息显示在页面某个地方)。 下...

    ajaxfileupload.js

    $.ajaxFileUpload({ url: url, type: 'post', data: data, secureuri: false, fileElementId: fileId, // input-file的id、name属性名 dataType: 'JSON', beforeSend: function (XMLHttpRequest) { //show ...

    解析ajax事件的调用顺序

    jquery的ajax请求方法:复制代码 代码如下:$.ajax({ type: “GET”, dateType:”html”, url: “index.html”, error: function(msg) { alert(“error”); }, complete: function(msg) { alert(...

    jaxify:浏览器服务器ajax调用代码生成器

    它强制开发人员使用从$.ajax返回的 promise 而不是success 、 error和complete属性。 这更好,因为 Promise 使代码更具可读性。 当您更改某些内容(即:url)时,您只需修改一个代码库。 这也是正确的,因为前一点...

    jquery插件使用方法大全

    $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); 编辑本段渐入淡出  代码 $("#msg").show("fast"); $("#msg...

    在jQuery1.5中使用deferred对象 着放大镜看Promise

    引言在那篇经典的关于jQuery1.5中Deferred使用方法介绍的文章中(译文见这里),有下面一段描述: $.ajax() returns an object packed with other deferred-related methods. I discussed promise(), but you’ll ...

    ajax-utilities:AJAX 辅助方法

    使用队列的 AJAX 帮助方法。 队列背后的逻辑:正在处理第一个发送的... 但是,如果您决定使用默认值,请记住,send() 中会覆盖 success() 和 error(),如果您正在使用队列,则 complete() 也会被覆盖。 使用风险自负。

    javascript util

    文件包含源代码和压缩版,压缩后只有9...success:function(result){alert("返回值"+result)}, error:function(xh){alert("发生错误:"+xh)}, complete:function(){alert("请求执行完成");} }); 等等还有其他便捷的函数...

    大名鼎鼎SWFUpload- Flash+JS 上传

     upload_success_handler : upload_success_function, 文件上传成功后触发的事件处理函数  upload_complete_handler : upload_complete_function,  debug_handler : debug_function,  custom_settings : { ...

    防止重复发送 Ajax 请求

    要考虑并理解 success, complete, error, timeout 这些事件的区别,并注册正确的事件,一旦失误,功能将不再可用; 不可避免地比普通流程要要多注册一个 complete 事件; 恢复状态的代码很容易和不相干的代码混合在...

    eBayjsonpipe.zip

     // Called after success/error, with the XHR status text  },  "timeout": 3000, // Number. Set a timeout (in milliseconds) for the request  "method": "GET", // String. The ...

    backbone-xhr-events:Backbone 插件为您提供全功能的 XHR 观察器功能

    提供健壮的生命周期事件( before-send 、 after-send 、 success 、 error 、 complete ) 发出特定于类型的 XHR 事件以允许聚焦绑定 能够查看模型当前是否有任何待处理的 XHR 活动 提供全局事件总线以绑定到所有...

Global site tag (gtag.js) - Google Analytics