骨干model.save()发送PUT POST而不是 [英] Backbone model.save() is sending PUT instead of POST
问题描述
我把保存使用这个:
的console.log(this.model.isNew());
的console.log(this.model);this.model.save({},{
成功:函数(模型,响应,期权){
的console.log(响应);
},
错误:功能(模型,XHR,期权){
的console.log(xhr.result.Errors);
}
});
的是否新款()
返回false。但 this.model
的输出具有0的ID(this.model.id为0以及)
我的网址网址:ROOTAREA +/费用/进入/
更新工作正常,如预期用途PUT。
编辑:这是我的模型的一部分:
默认设置:功能(){
返回{
DocumentDate:,
JobNo_:,
相code:
WorkType code:
说明:
数量:0,
UnitCost:0,
ExpenseCurrency code:
ReimbursementCurrency code:
UnitofMeasure code:DIEM
LineNo_:0
};
},
idAttribute:LineNo_
ID甚至不应该存在一个新的条目。
问题是你没有显示的部分 - 在实例化,创建和填充模型中的一部分
下面是骨干文档:
如果模型还没有一个
ID
,它被认为是新的。
块引用>这是从code,你是分配一个id属性明确。
后端应该这样做。
而且,由于你是一个客户端上做,骨干presumes它,它不是新的,并且使用PUT
I call save using this:
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); } });
The
isNew()
returns false. But the output ofthis.model
has an ID of 0. (this.model.id is 0 as well)My url is
url: ROOTAREA + "/Expenses/Entry/",
Updating works fine, and uses PUT as expected.
Edit : here's part of my model:
defaults: function () { return { DocumentDate: "", JobNo_: "", PhaseCode: "", WorkTypeCode: "", Description: "", Quantity: 0, UnitCost: 0, ExpenseCurrencyCode: "", ReimbursementCurrencyCode: "", UnitofMeasureCode: "DIEM", LineNo_: 0 }; }, idAttribute: "LineNo_",
解决方案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.
Here is a quote from the Backbone documentation:
If the model does not yet have an
id
, it is considered to be new.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
这篇关于骨干model.save()发送PUT POST而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!