0

在我的骨干收藏中,我有

fetch_data: function(data, callback) {
  this.fetch({
    data: data,
    success: callback,
    error: function() {
      // detect of HTTP code here
    }
  });
}

我想检查错误中的 HTTP 标头状态代码。如何在错误函数中访问 XHR 对象?

4

2 回答 2

2

http://backbonejs.org/#Model-fetch

看起来好像 error 被传递了一个 xhr 对象作为它的第二个参数。

因此,将您的代码更改为如下所示:

fetch_data: function(data, callback) {
  this.fetch({
    data: data,
    success: callback,
    error: function(model, xhr, options) {
      // do something with the xhr argument here. 
    }
  });

}

于 2013-01-09T00:18:59.493 回答
1

这应该适合你:

fetch_data: function(data, callback) {
  this.fetch({
    data: data,
    success: callback,
    error: function(model,resp) {
      console.log(resp.status);   // 404..etc.
    }
  });
}
于 2013-01-09T00:22:05.153 回答