繁体   English   中英

ajax调用返回的jqXHR对象

[英]The jqXHR object returned by the ajax call

成功事件或错误事件都会得到返回的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);
     }
   });

错误事件的输出得到 jqXHR.status: 0。成功事件的输出是 textStatus: [object Object] jqXHR.status: undefined。

来自 jQuery ajax文档

成功

类型:函数(任何数据,字符串 textStatus,jqXHR jqXHR) ...

所以,如果你想在success回调中访问jqXHR对象,你需要定义三个参数让函数接受,如下所示:

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

如果要取回提交的数据,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