Backbonejs集合获取错误检测XHR对象

时间:2013-01-09 00:14:09

标签: javascript jquery ajax backbone.js

在我的骨干系列中,我有

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

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

2 个答案:

答案 0 :(得分:2)

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

看起来错误是将xhr对象作为其第二个参数传递。

因此,请将代码更改为:

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

}

答案 1 :(得分:1)

这应该适合你:

fetch_data: function(data, callback) {
  this.fetch({
    data: data,
    success: callback,
    error: function(model,resp) {
      console.log(resp.status);   // 404..etc.
    }
  });
}
相关问题