我有一个想要获取的 Backbone 集合和模型数组。当我调用 fetch 时,ajax 请求会到达正确的休息端点。但是,我的集合的解析函数从未被调用。我不确定 json 最终在哪里,但我确实在 chrome 的网络选项卡中得到了带有有效负载的 200。我应该采取不同的做法吗?我过去曾这样做过(主干 1.0,现在主干 1.1)并且工作正常,所以我不确定问题是什么?
new MonitorCollection(app);
this.collection.fetch(); // this makes the correct ajax call
集合:
define([
'jquery',
'underscore',
'backbone',
'model/monitormodel',
], function ($, _, Backbone, MonitorModel) {
var collection = Backbone.Collection.extend({
initialize: function (app, options) {
this.app = app;
},
url: "api/monitor?since=10",
model: MonitorModel,
parse: function (response) {
console.log(response);
return response;
}
});
return collection;
});
型号:
define([
'jquery',
'underscore',
'backbone',
'view/monitordeviceview',
], function ($, _, Backbone, MonitorDeviceView) {
var model= Backbone.Model.extend({
initialize: function (app, options) {
this.app = app;
this.view = new MonitorDeviceView(this.app);
this.view.render(this);
},
parse: function (response) {
console.log(response);
return response;
}
});
return model;
});
最佳答案
问题是发送回的 json 不是有效的 json:NaN 在 json 中无效。
{
"deviceId": "ac867418c110",
"totalProbeRequestCount": 0,
"errorRate": NaN,
"errorCount": 0,
"succcesRate": NaN,
为了帮助调试此添加错误处理程序并阅读链接:
这些链接应该有助于理解主干网如何与服务器交互。
var collection = Backbone.Collection.extend({
initialize: function (options) {
this.on("error", this.error, this)
this.fetch();
},
url: "api/monitor?since=10",
model: MonitorModel,
parse: function (data) {
console.log(data);
return data.items;
},
error: function (model, response, options) {
console.log(model);
console.log(response);
console.log(options);
}
});
关于javascript - 主干解析ajax响应函数没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20304771/