【问题标题】:What's the difference between a 200 and a 200 OK in chrome?chrome 的 200 和 200 OK 有什么区别?
【发布时间】:2018-12-11 01:19:18
【问题描述】:

我在 chrome 网络日志中的一些 XHR GET 请求旁边没有看到 200 OK 状态代码。只有200没有OK。这是什么意思,有什么影响? notice the 200 statuses with no OK

还有其他人遇到过这个问题吗?我在想这是因为当我触发这些获取请求时,我也会收到 CORS 错误,如 here. 所见 @ 请求是否通过?

【问题讨论】:

    标签: google-chrome get xmlhttprequest google-chrome-devtools


    【解决方案1】:

    它不依赖于 chrome。服务器发送对请求的响应。它以 HTTP 协议版本 (HTTP/1.1)、状态代码 (200) 和状态消息开始(代码 200 通常可以)。示例响应:

    HTTP/1.1 200 OK
    

    另一个例子

    HTTP/1.1 404 Not Found
    

    但是没有定义响应 200 必须是 OK 和 404 Not Found。它可以是任何东西。例如代码 200 的另一个有效响应。

    HTTP/1.1 200 My own status message
    

    您可以编写简单的 node.js 服务器,它发送 我自己的状态消息 而不是 OK

    var http = require('http');
    
    http.createServer(function (req, res) {
        res.statusMessage = "My own status message"; // setting custom status message
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Hello World\n');
    }).listen(1337);
    

    并在开发者工具中查看

    所以答案是:如果响应显示 OK 或其他内容,或者什么都没有,则没有区别。 200 表示 OK,这是最重要的信息。状态消息只是 200 的人类可读转录,对浏览器并不重要。

    【讨论】:

    • 我认为这不会影响像这样检查 OK:fetch('https://someurl') .then(res => { if(res.ok) { return res; } else { throw Error(Request denied with status ${res.status}); } }) .catch(console.error) ?
    • @joetidee res.ok 在状态码为 200 且与状态消息无关时为真。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多