未调用主干解析ajax响应函数

Backbone parse ajax response function not being called

本文关键字:ajax 响应 函数 调用      更新时间:2024-01-09

我有一个主干集合,我想获取它和模型数组。当我调用fetch时,ajax请求会命中正确的rest端点。但是,我的集合的解析函数从未被调用。我不确定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);
        }
    });