stoefln
stoefln

Reputation: 14575

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 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?

Upvotes: 1

Views: 156

Answers (2)

Darrel Miller
Darrel Miller

Reputation: 142222

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

Upvotes: 3

Alex Reynolds
Alex Reynolds

Reputation: 96974

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.

Upvotes: 0

Related Questions