【问题标题】:Suitable HTTP Status Code合适的 HTTP 状态码
【发布时间】:2011-06-14 04:43:08
【问题描述】:

我的 Web 应用程序使用 ajax,我通过 php 代码检查请求是否为 ajax 请求。如果不是,那么我会生成 404 错误,否则运行与 ajax 函数相关的 php 代码。

如果用户或搜索蜘蛛试图访问 ajax 功能页面(例如:/books/ajax/books_list)网络应用返回 404 not found 状态代码,我在 google 网站管理员工具中看到很多 404 错误。

我应该更改 404 状态码,但哪个适合这种情况?可以是“406不可接受”吗?

【问题讨论】:

  • 仅供参考,“编程代码”上下文中的“代码”是不可数名词; “php 代码”是错误的。

标签: http-status-code-404 http-status-codes


【解决方案1】:

我认为 403 (Forbidden) 可能最能描述它。资源在那里,但您已确定不授予对该资源的访问权限,甚至验证用户身份也无济于事。

【讨论】:

    【解决方案2】:

    我建议您返回您建议的 406。唯一值得考虑的替代方案是“501 未实施”

    The server does not support the functionality required to fulfill the request.
    This is the appropriate response when the server does not recognize the request
    method and is not capable of supporting it for any resource
    

    还有“403 禁止”

    The server understood the request, but is refusing to fulfill it. 
    Authorization will not help and the request SHOULD NOT be repeated. 
    If the request method was not HEAD and the server wishes to make
    public why the request has not been fulfilled, it SHOULD describe 
    the reason for the refusal in the entity. If the server does not 
    wish to make this information available to the client, the status
     code 404 (Not Found) can be used instead. 
    

    【讨论】:

      【解决方案3】:

      不确定您如何确定它是否是正确的 Ajax 请求。如果您希望它是 POST,但蜘蛛使用 GET,那么它应该是 405(不允许的方法)。

      您不应该使用 406:它表示“不可接受”,因为您不能支持浏览器发送的 HTTP Accept: 标头。这可能是不正确的(因为您可能根本没有检查 Accept 标头)。

      如果您真的拒绝请求,因为它来自未经授权的客户端,那么 403 是合适的。

      【讨论】:

        【解决方案4】:

        我的理解是,如果 SE 蜘蛛看到表明服务器问题的错误,它们会有点紧张。 (406 可能表示服务器实施不当。) 404 没有描述您要查找的内容,因为资源在那里,但 403(禁止)只是让蜘蛛知道此页面不适合他们。您也可以使用您的 robots.txt 文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-14
          • 1970-01-01
          • 2011-04-06
          • 2015-12-29
          • 2010-12-15
          • 2013-04-04
          • 2014-09-30
          • 1970-01-01
          相关资源
          最近更新 更多