简体   繁体   English

ajax调用返回的jqXHR对象

[英]The jqXHR object returned by the ajax call

Either the success event or the error event will get the returned jqXHR object, but I can only access the jqXHR object in the error event.成功事件或错误事件都会得到返回的jqXHR对象,但我只能在错误事件中访问jqXHR对象。

    $.ajax({
       type: 'POST',
       url:'https://fakeurl',
       data: formData,
       contentType: 'application/x-www-form-urlencoded',                     
       dataType: 'json',
       success: function(textStatus, jqXHR) {
           alert('textStatus: ' + textStatus + ' jqXHR.status: ' + jqXHR.status);
     },error: function(jqXHR) {
       console.log('jqXHR.status: ' + jqXHR.status);
     }
   });

The output in error event got jqXHR.status: 0. And the output in success event is textStatus: [object Object] jqXHR.status: undefined.错误事件的输出得到 jqXHR.status: 0。成功事件的输出是 textStatus: [object Object] jqXHR.status: undefined。

From the jQuery ajax docs :来自 jQuery ajax文档

success成功

Type: Function( Anything data, String textStatus, jqXHR jqXHR ) ...类型:函数(任何数据,字符串 textStatus,jqXHR jqXHR) ...

So, if you want to access the jqXHR object in the success callback, you'll need to define three parameters for the function to accept like so:所以,如果你想在success回调中访问jqXHR对象,你需要定义三个参数让函数接受,如下所示:

success: function(data, textStatus, jqXHR) {
           alert('data: ' + data + 'textStatus: ' + textStatus + ' jqXHR.status: ' + jqXHR.status);

if you want to get back the submitted data, the first argument of success function is the submitted data and the second is the textStatus, the third one for jqXHR which that has all the properties of the response object如果要取回提交的数据,success 函数的第一个参数是提交的数据,第二个参数是 textStatus,第三个参数是 jqXHR,它具有响应对象的所有属性

$.ajax({
   type: 'POST',
   url:'https://fakeURL',
   data: formData,
   contentType: 'application/x-www-form-urlencoded',
   dataType: 'json',
   success: function(data, textStatus, jqXHR) {
      alert('textStatus: ' + textStatus + ' jqXHR: ' + jqXHR.status);
 },error: function(error) {
   console.log('error.status: ' + error.status);
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM