RESTful ajax-“未找到项目"的哪个HTTP状态代码? [英] RESTful ajax - which HTTP status code for "item not found"?

查看:69
本文介绍了RESTful ajax-“未找到项目"的哪个HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个单页Web应用程序,其中包含项目列表,用户可以在其中单击项目以查看有关该项目的更多信息.使用对URL http://www.example的URL的AJAX GET请求,异步加载实际的项目信息..com/item/[id] ,并在加载时显示.

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.com/item/[id] and is displayed upon loading.

问题是,如果给定ID的商品不存在,该怎么办?现在,该请求返回状态码200,但实际内容表明该商品不存在.这似乎是个坏主意,所以我想让它返回一个状态代码,该状态代码在某种意义上表示请求失败,因为该项目不存在(例如,它也允许我将其捕获为失败" 处理程序.

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).

这是一种好习惯吗(仅当该项目实际存在于数据库中时才返回状态代码200),如果是,那么在这种情况下哪种状态代码是最佳的使用方式?

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?

推荐答案

是的,您应该使用故障状态代码,而不是200 OK.在这种情况下,"404未找到"将是理想的选择.(用于用于的商品也有410个"Gone".)

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.)

链接到规范,或使用Google"HTTP状态代码"获取更多资源.

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

这篇关于RESTful ajax-“未找到项目"的哪个HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆