Javascript 重新建立连接时,xmlhttprequest状态不会更改

Javascript 重新建立连接时,xmlhttprequest状态不会更改,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我想发布一个http请求,直到收到肯定的响应。如果我的笔记本电脑上的wifi打开,我会得到肯定的响应,程序就会退出(正确)。如果我的笔记本电脑上的wifi最初是关闭的,但在程序仍在尝试时打开,我不会得到积极的响应,程序会一直尝试并失败,直到退出。当wifi打开时,nt xmlhttp.status是否应为200 xmlhttp = new XMLHTTPRequest(); xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader('Us

我想发布一个http请求,直到收到肯定的响应。如果我的笔记本电脑上的wifi打开,我会得到肯定的响应,程序就会退出(正确)。如果我的笔记本电脑上的wifi最初是关闭的,但在程序仍在尝试时打开,我不会得到积极的响应,程序会一直尝试并失败,直到退出。当wifi打开时,nt xmlhttp.status是否应为200

xmlhttp = new XMLHTTPRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');

data = "Some text here";
xmlhttp.send(Base64.encode(data));

var timeout = 16;
var response = '';

for (var t = 0; t < timeout; t++) {

    if (xmlhttp.ReadyState == 4) {

        // If XMLHTTPRequest returns status code 200 (OK) and the response text contains REPORT_SUCCESS, it means that the report was successful.
        if (xmlhttp.Status == 200 && xmlhttp.ResponseText.indexOf(REPORT_SUCCESS) != -1) {

            return true;
        } else {
            if (xmlhttp.Status == 200) {
                // if the XMLHTTPRequest returns status code 200 (ok) and the response text does not contain REPORT_SUCCESS
                break;
            } else {
                //try again 
            }
        }
    }

    WScript.Sleep(1000);
}
xmlhttp=newxmlhttprequest();
open(“POST”,url,true);
setRequestHeader('User-Agent','xmlhttp/1.0');
data=“此处有一些文本”;
send(Base64.encode(data));
var超时=16;
var响应=“”;
for(var t=0;t
xmlhttp已完成请求,并在wifi关闭时收到错误响应。当wifi打开时,它不会自动重新尝试请求。这必须由你自己来做

ReadyState=4和状态!=循环中始终有200个


如果xmlhttp开始发送请求并同时打开wifi,则您的代码将正常工作。

我不确定是否理解。我移动了语句xmlhttp.send(Base64.encode(data));在for循环中,它仍然无法工作。如果重试,请使用新的xmlhttp in循环