未捕获的TypeError:将循环结构转换为JSON:Backbone

时间:2013-06-29 09:37:24

标签: json backbone.js marionette backgrid

我正在尝试保存骨干集合,如下所示:

var backgridModel = Backbone.Model.extend({ });

var BackgridCollection = Backbone.Collection.extend({
    model: backgridModel,
    url : 'api/list'
});

var backgriCollections = new BackgridCollection();
backgriCollections.fetch();

var CreatePuppetRequest = Backbone.Model.extend({
    url : 'api/createTable',
    toJSON: function() {
        console.log(backgriCollections.toJSON());
        return backgriCollections.toJSON();
    }
});

我按如下方式调用服务器:

var puppetTable = new CreatePuppetRequest();
puppetTable.save({}, {
    success : function(model, response) {
        alert("Successfully submitted " + num + " puppet(s)");
        puppetFinalCollection.reset();
        $('#create-puppet-table-button').removeAttr("disabled");
    },
    error : function(model, response) {
        if (response.status == 400)
            that.showErrors($.parseJSON(response["responseText"]));
        else
            console.log(response["responseText"]);
        puppetFinalCollection.reset();
        $('#create-puppet-table-button').removeAttr("disabled");
    }
});

控制台输出

0: Object
designation: "m"
firstName: "sdfghj"
function (){ return parent.apply(this, arguments); }: Object
lastName: "sdfghj"
__proto__: Object
length: 1
__proto__: Array[0]

function (){ return parent.apply(this, arguments); }: Object被添加到模型中,我不知道为什么要添加它?

除此之外,我还得到了Uncaught TypeError: Converting circular structure to JSON来阻止服务器调用。

修改

从服务器获取fetch之后

我的数据看起来像下面的JSON字符串:

[
    {
        "userid": "",
        "firstName": "Mayank",
        "lastName": "Kumar",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Shylendra",
        "lastName": "Bhat",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Akash",
        "lastName": "Waran",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Shreyas",
        "lastName": "Lokkur",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Anthony",
        "lastName": "Raj",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Dheemanth",
        "lastName": "Bharadwaj",
        "designation": "Software Engineer"
    },
    {
        "userid": "",
        "firstName": "Prasanna",
        "lastName": "Adiga",
        "designation": "Software Engineer"
    }
]

并且它也被填充到表格中。

1 个答案:

答案 0 :(得分:-1)

当我将模型添加到集合时,我犯了错误,我添加了一个类型而不是该类型的实例。

var backgridModel = Backbone.Model.extend({ });

是我应该添加的类型

var row = new backgridModel()我使用backgridModel创建了一个新行。