开发者

how to return the ID of a resource which was created by ajax HTTP POST to the client

开发者 https://www.devze.com 2023-03-05 20:53 出处:网络
I\'m wondering whether there is a standardized way to get the identity (id/hash/whatever) of the开发者_JAVA百科 object/resource which was just created on the server via POST. Is there some HTTP header

I'm wondering whether there is a standardized way to get the identity (id/hash/whatever) of the开发者_JAVA百科 object/resource which was just created on the server via POST. Is there some HTTP header which can be set?

I'm using jquery right now, and what I do is to output the id of the created object into a hidden html div. After getting the response on the client, I parse the html, and retrieve the id. That seems kind of awkward to me. Any ideas?


The Location header is designed specifically for this purpose. When you create a resource, the origin server should return 201 and the Location header should contain the URL to the newly created resource.

See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30


Perhaps you could return a JSON list object, with an id key pointing to an id value, and a rawHTML key that contains the raw HTML that you want to pour into the div. Then you parse the JSON response for the keys' values.

0

精彩评论

暂无评论...
验证码 换一张
取 消