how to return the ID of a resource which was created by ajax HTTP POST to the client
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.
精彩评论