gpt4 book ai didi

ajax - Ember Data 在应该 PUT 时更新 POST

转载 作者:可可西里 更新时间:2023-11-01 16:18:09 26 4
gpt4 key购买 nike

我正在开发 Ember.js 应用程序。我有一个更新功能,它是 ObjectController 的一部分。

该函数应该保存我更新的模型,但是当我调用 save(); 时,它发送的是 POST 请求而不是 PUT 请求。 (在 Chrome 中测试。)

为什么会这样?如何确保发送 PUT 请求以获取更新?

这是我的代码:

customer = this.get('model');
customer.set('name', 'New name');
customer.save();

作为额外引用,当我使用 console.log( customer.get('dirtyType') ); 记录“dirtyType”时,它显示“已更新”。

非常感谢任何帮助!

更新

我已经调整了上面的示例代码以使其更清晰,我不是在创建新模型并想使用 PUT。我有一个现有模型需要更新。

最佳答案

我不确定您的解决方法在 PUT 和 POST 领域是否正确。

TL;DR PUT 应该定义资源(通过 Request-URI),但我们在创建期间不这样做,所以我们不应该使用 POST。如果您的服务器需要它,请覆盖创建/保存,而不是修改 isNew 属性,这可能会反噬您。

9.6 PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI,

自定义适配器

App.ApplicationAdapter = DS.RESTAdapter.extend({
createRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);

serializer.serializeIntoHash(data, type, record, { includeId: true });

//return this.ajax(this.buildURL(type.typeKey), "POST", { data: data });
return this.ajax(this.buildURL(type.typeKey), "PUT", { data: data });
},


updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);

serializer.serializeIntoHash(data, type, record);

var id = get(record, 'id');
// you could do the same here, but it's even more incorrect
return this.ajax(this.buildURL(type.typeKey, id), "PUT", { data: data });
},
});

http://www.ietf.org/rfc/rfc2616.txt

关于ajax - Ember Data 在应该 PUT 时更新 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24351916/

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