gpt4 book ai didi

javascript - Backbone save() 方法 Post 而不是 Put

转载 作者:行者123 更新时间:2023-12-01 03:45:51 24 4
gpt4 key购买 nike

我正在尝试更新主干上的数据。但是我的 save() 函数是 post 而不是 put。

editEvent:function(e){
var departments=new Department();
departments.set("departmentName",this.$el.find(".departmentName").val());
departments.save();

this.render();

},

部门模型

    var Department = Backbone.Model.extend({
urlRoot: "/rest/department",
idAttribute:'departmentID'

}
});
return Department;

模板

    <th>
<span>{{user.department.departmentName}}</span>
<input data-placement="top" title="Boş geçilemez!" class="form-control text-center departmentName" type='text'
style='display: none;' value='{{user.department.departmentName}}'/>
</th>

最佳答案

这是设计使然,这是 Backbone docs for the save function 的片段:

If the model isNew, the save will be a "create" (HTTP POST), if the model already exists on the server, the save will be an "update" (HTTP PUT).

如果模型没有id,则被视为"new"。

由于您是第一次创建模型,Backbone 将按其应有的方式使用 POST 方法,因为这意味着您正在创建新资源。欲了解更多信息meaning of HTTP verbs .

我的建议是在服务器端解决这个问题,并为 POST 和 PUT 制作单独的处理程序,但如果您确实想强制 PUT 方法,这里有一些提示:What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?

关于javascript - Backbone save() 方法 Post 而不是 Put,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43576460/

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