json - 未捕获的类型错误 : Converting circular structure to JSON : Backbone

标签 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 这也阻止了服务器调用。

编辑

从服务器获取数据后,我的数据将类似于以下 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"
    }
]

它也被填充到表中。

最佳答案

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

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

是我应该添加的类型

var row = new backgridModel() 而不是我使用 backgridModel 来创建一个新行。

关于json - 未捕获的类型错误 : Converting circular structure to JSON : Backbone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17378789/

相关文章:

python - 报废数据并推送到 Elasticsearch 时出现SerializationError

javascript - 我该如何重写这段代码

javascript - RESTful Backbone 应用程序中 JSON 的预期结构是什么?

javascript - 将参数传递给 Marionette 组件?

marionette - 如何在 Backbone.Marionette 中设置多个路由器和 Controller

javascript - Marionette - modelEvents 不起作用

json - 如何替换 json 字符串和正则表达式中的双引号?

json - Golang Json编码

php - 在 Codeigniter 中设置跨域

javascript - 使用变量作为参数键 Javascript/Backbone.js