简体   繁体   English

Backbone model.save在状态码200上调用错误函数

[英]Backbone model.save calls error function on status code 200

this.model.save(newModel, {error: function (e){
        alert("Error trying to save contact: " + e);
        console.log(e);
    }});

Above is the code that is running on the client side. 以上是客户端上运行的代码。 the server code is 服务器代码是

model.update_contact(contact, function(err){
        if(err){
            res.statusCode = 500;
            return res.json({"Database error" : err});
        }
        return res.end();
    });

inspecting the network traffic i see that the server responds with status code 200 but the web page shows the alert message "Error trying to save contact: [Object object]" 检查网络流量我看到服务器响应状态代码200,但网页显示警告消息“错误尝试保存联系人:[对象对象]”

ps the db query is successful ps db查询成功

Backbone expects to get the model's JSON back in response to PUT or POST requests IIRC. Backbone希望在响应PUT或POST请求IIRC时恢复模型的JSON。

Instead of returning res.end() try this: 而不是返回res.end()尝试这个:

return res.json(contact);

Dumb mistake, my server was not properly returning the object back. 愚蠢的错误,我的服务器没有正确返回对象。 Once i added code to return the object everything ran fine. 一旦我添加代码返回对象一切运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM