user2442193
user2442193

Reputation: 1

xmlhttprequest status does not change when connection is re-established

I want to post an http request until I receive a positive response. If the wifi on my laptop is on, I get a positive response and the program exits (correct). If the wifi on my laptop is initially off but turned on while the program is still trying, I do not get a positive response and the program keeps trying and failing until it exits. Should'nt xmlhttp.status = 200 when the wifi is turned on?

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);
}

Upvotes: 0

Views: 248

Answers (1)

comphilip
comphilip

Reputation: 550

xmlhttp completed request and got an error response when wifi was off. It will not re-try automatically request when wifi turned on. It has to be done by your self.

ReadyState = 4 and Status != 200 in the loop all the time.

Your code will work if xmlhttp start to sending request and wifi turned on at the same time.

Upvotes: 1

Related Questions