对于仅 AJAX URL 的非 AJAX 请求,我应该返回什么状态代码?

发布于 2024-10-21 20:20:41 字数 40 浏览 2 评论 0原文

如果不使用 AJAX 请求 URL,应返回什么状态代码? 400?

If the URL is requested not using AJAX what status code should be returned? 400?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

碍人泪离人颜 2024-10-28 20:20:41

我认为 403 Forbidden 是最好的。

501 Not Implemented 不太适合,因为资源已实现,但不适合这种请求。

400 Bad Request 也不是一个好的选择,因为根据规范,这意味着“由于语法错误,服务器无法理解该请求”,但这里的情况并非如此。

I think 403 Forbidden ist best.

501 Not Implemented does not fit well, because the resource is implemented, but just not suited for that kind of request.

400 Bad Request ist not a good choice either, because this implies, according to the spec, that "the request could not be understood by the server due to malformed syntax", which is not the case here.

清醇 2024-10-28 20:20:41

我不相信你通过监管这个价值观真的能帮助任何人。我更喜欢在所有情况下都尝试发回有用数据的理念。然而,不难想象我们确实需要一项政策的类似情况。例如,假设我们有一个可以根据 mime 类型返回 JSON 或 XML 的服务,如果指定了意外的 mime 类型,也许最好返回一个清晰的响应代码。

W3

4xx 代码适用于案例
其中客户似乎有
错误,以及案例的 5xx 代码
其中服务器知道
服务器出错了。这是不可能的
一般来说区分这些情况,所以
差异仅供参考。

主体部分可能包含
描述人为错误的文件
可读形式。该文档采用 MIME 格式
格式,并且只能是文本/纯文本,
text/html 或一种格式
指定为可接受的
请求。

因此,很明显,响应应该是 4xx,而不是客户端提供无效请求。我会选择 400 BAD REQUEST 并确保返回的文本解释了为什么请求被认为是错误的。

W3 对于 400 表示:请求语法错误或本质上不可能得到满足,情况似乎确实如此。

I'm not convinced that you're really helping anyone by policing this value. I'd prefer a philosophy of trying to send back useful data in all cases. However, it's not much of a stretch to imagine similar situations where we do need a policy. For example, suppose we have a service that can return JSON or XML depending upon the mime type, perhaps it is better to return a clear response code if an unexpected mime type is specified.

W3 say

The 4xx codes are intended for cases
in which the client seems to have
erred, and the 5xx codes for the cases
in which the server is aware that the
server has erred. It is impossible to
distinguish these cases in general, so
the difference is only informational.

The body section may contain a
document describing the error in human
readable form. The document is in MIME
format, and may only be in text/plain,
text/html or one for the formats
specified as acceptable in the
request.

And so it seems clear that the response should be a 4xx, it't the client supplying an invalid request. I'd go with 400 BAD REQUEST and make sure that the text returned explains why the request is considered bad.

W3 says for 400: The request had bad syntax or was inherently impossible to be satisfied, and that does indeed seem to be the case.

┾廆蒐ゝ 2024-10-28 20:20:41

我会选择 400 Bad Request,因为它意味着不重试请求。

如果做不到这一点,418 我是一个茶壶,只是为了迷惑他们:)

I'd go 400 Bad Request, since it has the implication not to retry the request.

Failing that, 418 I'm a teapot, just to confuse 'em :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文