Backbone model.save() 正在发送 PUT 而不是 POST [英] Backbone model.save() is sending PUT instead of POST

查看:13
本文介绍了Backbone model.save() 正在发送 PUT 而不是 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个调用保存:

console.log(this.model.isNew());
console.log(this.model);

this.model.save({}, {
    success: function (model, response, options) {
        console.log(response);
    },
    error: function (model, xhr, options) {
        console.log(xhr.result.Errors);
    }   
});

isNew() 返回 false.但是this.model的输出ID为0.(this.model.id也是0)

The isNew() returns false. But the output of this.model has an ID of 0. (this.model.id is 0 as well)

我的网址是 url: ROOTAREA + "/Expenses/Entry/",

更新工作正常,并按预期使用 PUT.

Updating works fine, and uses PUT as expected.

这是我模型的一部分:

   defaults: function () {
        return {
            DocumentDate: "",
            JobNo_: "",
            PhaseCode: "",
            WorkTypeCode: "",
            Description: "",
            Quantity: 0,
            UnitCost: 0,
            ExpenseCurrencyCode: "",
            ReimbursementCurrencyCode: "",
            UnitofMeasureCode: "DIEM",
            LineNo_: 0
        };
    },
    idAttribute: "LineNo_",

推荐答案

ID 甚至不应该存在于新条目中.问题出在您没有显示的部分 - 在您实例化、创建和填充模型的部分.

ID should not even exist for a new entry. The issue is in the part you didn't show - in the part where you instantiate, create and populate the model.

这里引用了 Backbone 文档:

如果模型还没有 id,则认为它是新的.

If the model does not yet have an id, it is considered to be new.

从您的代码中可以清楚地看出您正在分配一个 id 属性.您的后端应该这样做.并且由于您是在客户端上执行此操作,因此主干假定它不是新的,并使用 PUT

It is clear from your code that you are assigning an id attribute. Your backend should be doing that. And since you are doing it on a client, backbone presumes it it not new, and uses PUT

这篇关于Backbone model.save() 正在发送 PUT 而不是 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆