javascript 为什么 XmlHttpRequest readyState = 2 on 200 HTTP 响应代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26419888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:56:58  来源:igfitidea点击:

Why XmlHttpRequest readyState = 2 on 200 HTTP response code

javascriptphpajaxxmlhttprequesthttp-status-code-200

提问by snowbound

So I'm using plain javascript (no jquery), to send a file to the server. Server script PHP returns status code 200 at the end, but instead javascript is getting readyState == 2.

所以我使用普通的javascript(没有jquery)将文件发送到服务器。服务器脚本 PHP 在最后返回状态代码 200,但 javascript 变得 readyState == 2。

The PHP code sends back status code 200:

PHP 代码发回状态代码 200:

header('X-PHP-Response-Code: 200', true, 200);
exit;

The javascript is doing:

javascript正在做:

request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var message;
            switch(request.status) {
                case '200':
                     message = "Data uploaded successfully.";
                break;

                case '406':
                    message = "Incorrect file format.  Please try again.";
                break;

                case '410':
                    message = "Unexpected error.  Please contact support.";
                break;

                default:
                break;
            }
            status_message_container.innerHTML = message;
            submit_button.disabled = false;
        }
        else {
            alert( "Unexpected error:  " + this.statusText + ".\nPlease try again");
        }
    };

    request.send(formData);

Even know the HTTP 200 status code comes back correctly (I get 'OK') on frontend. The JS script is seeing readyState==2(i.e. else block always hit)

甚至知道 HTTP 200 状态代码在前端正确返回(我得到“OK”)。JS脚本看到readyState==2(即else块总是命中)

My understanding is that a server status code of 200 should give readyState == 4??

我的理解是服务器状态代码 200 应该给出readyState == 4??

回答by CodingIntrigue

Firstly, onreadystateisn't just fired once. It's fired multiple times, you need to be able to handle that. These are the codes you need to handle:

首先,onreadystate不只是被解雇一次。它被多次触发,你需要能够处理它。这些是您需要处理的代码:

0 UNSENT - open()has not been called yet
1 OPENED - send()has not been called yet
2 HEADERS_RECEIVED - send() has been called, and headers and status are available
3 LOADING Downloading; - responseText holds partial data
4 - The operation is complete

0 UNSENT - open() 还没有被调用
1 OPENED - send() 还没有被调用
2 HEADERS_RECEIVED - send() 已经被调用,并且头部和状态可用
3 LOADING 下载;- responseText 保存部分数据
4 -操作完成

Your code is hitting the else block on readyState == 2(headers received) and assuming that is an error status when it's not.

您的代码遇到了 else 块readyState == 2(收到的标头),并假设它不是错误状态。

You error check should be inside the request.readyState == 4check. That way, the request is complete but there could also have been an error:

你的错误检查应该在request.readyState == 4检查里面。这样,请求就完成了,但也可能出现错误:

if (request.readyState == 4) {
    switch(request.status) {
        case '200':
            message = "Data uploaded successfully.";
        break;
        // Error handling here
        default: alert( "Unexpected error:  " + this.statusText + ".\nPlease try again"); break;
    }
}

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest