gpt4 book ai didi

backbone.js - 如何从保存模型中获得良好的响应

转载 作者:行者123 更新时间:2023-12-04 02:00:24 25 4
gpt4 key购买 nike

使用backbone.js,我正在保存一个模型。将 PUT 发送到服务器并返回响应。我第一次这样做时,它返回成功,以下时间返回错误,因为在第一次将响应添加到模型之后。

Backbone.js 中的保存函数:

saveCountry: function() {
this.model.save({},{
success: function(model, response) {
console.log('success! ' + response);
},
error: function(model, response) {
console.log('error! ' + response);
}
});
this.render();

return false;
},

PHP 返回一个 JSON 字符串:
{"response": "Model saved!"}

在 PUT 之后得到一个错误作为响应,因为“响应”被添加到模型中:
Unknown column 'response' in 'field list'

为什么将响应添加到模型中以及如何防止它?

最佳答案

来自 Backbone 关于模型的文档 save :

Set a hash of model attributes, and sync the model to the server. If the server returns an attributes hash that differs, the model's state will be set again. http://documentcloud.github.com/backbone/docs/backbone.html#section-41



如何使它工作:不要从服务器返回 {"response": "Model saved!"} 。只返回一个没有内容的成功(状态 200)。

如果保存不起作用,返回一个包含错误的 JSON,Backbone 将在您的模型上触发一个错误事件,使用您提供的 JSON(参见 http://documentcloud.github.com/backbone/docs/backbone.html#section-41http://documentcloud.github.com/backbone/docs/backbone.html#section-145)。

关于backbone.js - 如何从保存模型中获得良好的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7836574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com