如何将ajax HTTP POST创建的资源的ID返回给客户端
我想知道是否有一种标准化的方法来获取刚刚通过 POST 在服务器上创建的对象/资源的身份(id/散列/其他)。是否有一些可以设置的 HTTP 标头?
我现在正在使用jquery,我所做的是将创建的对象的id输出到隐藏的html div中。在客户端收到响应后,我解析 html,并检索 id。这对我来说似乎有点尴尬。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Location 标头是专门为此目的而设计的。创建资源时,源服务器应返回 201,并且 Location 标头应包含新创建资源的 URL。
请参阅:http://www.w3.org/Protocols/rfc2616 /rfc2616-sec14.html#sec14.30
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
也许您可以返回一个 JSON 列表对象,其中一个
id
键指向一个id
值,以及一个rawHTML
键,其中包含原始 HTML你想倒入div中。然后解析 JSON 响应以获取键的值。Perhaps you could return a JSON list object, with an
id
key pointing to anid
value, and arawHTML
key that contains the raw HTML that you want to pour into the div. Then you parse the JSON response for the keys' values.