如果请求失败,我的错误事件处理程序如何识别哪些特定数据集无法上载?
error(XMLHttpRequest,textStatus,errorThrown)
textStatus或errorThrown似乎都不能用于识别失败的数据集,而XHR似乎也没有提供这种能力.
解决方法
this
…从文档中 – 注意突出显示的部分:
The
beforeSend
,error
,dataFilter
,success
andcomplete
options all take callback functions that are invoked at the appropriate times. Thethis
object for all of them will be the object in thecontext
property passed to$.ajax
in the settings; if that was not specified it will be a reference to the Ajax settings themselves.
因此,在error()回调(也是成功和完成等)中,这将是传递给$.ajax()的对象 – 除非您使用context参数来更改它.
注意,传入$.ajax()的数据参数将转换为GET或POST请求的序列化字符串
另一个选择是确保你的error()回调可以访问同一范围内的变量:
(function() { // create a scope for us to store the data: var data = {id: 1}; $.ajax({ url: '/something-to-genereate-error',data: data,error: function() { console.log(this,data); } }); })(); // call the function immediatey
See this fiddle for an example – 请注意,其中的数据是“id = 1”,因为我们持有的数据仍为{id:1}
另外 – 注意这里的闭包((function(){….})())几乎没必要,它只是显示了一种方法来存储数据变量,然后将它传递到$.ajax()和使用它在回调中.很可能这个ajax调用已经存在于你可以做到这一点的函数中.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。