在 .fail 情况下捕获 $.deferred 的第三个参数输出

Capture 3rd param output of $.deferred in a .fail situation

我正在使用这段代码来捕获 jsonp 错误,它运行良好,但在这样调用时却不行。

this.promise = this.model.fetch();
.
.
.
.fail(_.bind(function(collection, response, options) {
   console.log('response', response);  //timeout
   console.log('options', options);  //timeout rather than object
})

在其他代码中,我们没有使用 .fail 而是错误:并且我确实在第 3 个参数上获得了对象。

顺便说一句,这是一个 jsonp 魔术 hack,因为它会在出现错误时删除 window 对象中的值。还有,故意执行超时错误。

然后在错误回调中,我有这段代码可以捕获它

if (options.jsonpCallback.indexOf('callback_') !== -1 ) {
  // jsonp magic code here
}

这不起作用。失败,因为选项的值只是一个字符串,即 statusText。值为"timeout"

对于 $.ajax 错误回调,您应该能够通过在回调中引用 this 来获取选项,前提是您不重新绑定 this:

.fail(function(xhr, status, error) {
   console.log('options', this);
})