返回什么错误,如果方法仅期望Ajax调用?错误、方法、Ajax

2023-09-10 19:33:37 作者:﹃ 凄凉过往

什么HTTP状态,如果它希望仅通过AJAX的使用应该采取行动的回报,但被称为没有适当的阿贾克斯头?我觉得我应该意味着一些错误,但我真的不能找到一个合适的。

What HTTP status should action return if it expects to only be used via AJAX, but was called without proper ajax headers? I feel I should signify some error, but I can't really find appropriate one.

我想最好是405不允许的方法,但它是奇怪,如果例如AJAX GET请求返回的内容,和普通GET将返回405

I guess best would be 405 Method not allowed, but it would be weird if for example ajax GET requests returned content, and plain GET would return 405.

推荐答案

您可以完成请求,但你拒绝这样做,因为它不是在AJAX,所以我觉得适当的错误是403 - 禁止

You can fulfill the request, but you refuse to do it since it's not in AJAX, so I think the appropriate error is 403 - forbidden.

http://www.w3.org/Protocols/ RFC2616 / RFC2616-sec10.html#sec10.4.4

反正,我不认为任何错误code将是奇怪的,如果它会伴随着一个明确的解释。

Anyways, I don't think any error code will be weird if it'll be accompanied with a clear explanation.