最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RESTful ajax - which HTTP status code for "item not found"? - Stack Overflow

programmeradmin4浏览0评论

Lets say I have a single-page web application with a list of items where a user can click on an item to view more info about it. The actual item info is loaded asynchronously, using an AJAX GET request to the URL /[id] and is displayed upon loading.

The question is, what to do if the item with the given id doesn't exist? Right now, the request returns status code 200, but the actual content indicates that the item doesn't exist. This seems like a bad idea so I wanted to make it return a status code that would indicate the request failed in a sense, since the item doesn't exist (it would also, for example, allow me to catch it in a fail handler in jQuery's ajax function).

Is this good practice (returning status code 200 only when the item actually exists in the database) and, if so, which status code would be the best to use in this situation?

Lets say I have a single-page web application with a list of items where a user can click on an item to view more info about it. The actual item info is loaded asynchronously, using an AJAX GET request to the URL http://www.example./item/[id] and is displayed upon loading.

The question is, what to do if the item with the given id doesn't exist? Right now, the request returns status code 200, but the actual content indicates that the item doesn't exist. This seems like a bad idea so I wanted to make it return a status code that would indicate the request failed in a sense, since the item doesn't exist (it would also, for example, allow me to catch it in a fail handler in jQuery's ajax function).

Is this good practice (returning status code 200 only when the item actually exists in the database) and, if so, which status code would be the best to use in this situation?

Share Improve this question asked Sep 16, 2014 at 12:20 fstanisfstanis 5,5541 gold badge25 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Yes, you are right to want to use a failure status code, not a 200 OK. 404 Not Found would be ideal in this case. (There is also 410 Gone for an item which used to exist.)

Link to spec, or Google "HTTP status codes" for far more resources.

Yes, it's a good idea to return 200 status code only when your object exists. When your item does not exist you should return a 404 error code.

More information here

发布评论

评论列表(0)

  1. 暂无评论