2014-06-19 35 views
0

我想發佈http請求,直到收到正面響應。如果我的筆記本電腦上的wifi開啓,我會得到積極的迴應,程序退出(正確)。如果我的筆記本電腦上的wifi最初是關閉的,但在程序仍在嘗試時打開,我沒有得到積極的響應,程序一直嘗試失敗,直到它退出。 wifi打開時應該不會xmlhttp.status = 200?重新建立連接後xmlhttprequest狀態不會更改

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

回答

1

xmlhttp完成請求並在WiFi關閉時收到錯誤響應。當wifi開啓時,它不會重新嘗試自動請求。它必須由你自己完成。

ReadyState = 4和Status!= 200一直在循環中。

如果xmlhttp開始發送請求並同時打開wifi,您的代碼將起作用。

+0

不知道我明白。我移動了語句xmlhttp.send(Base64.encode(data));在for循環中,它仍然無法工作。 – user2442193

+0

如果再次嘗試 – comphilip

+0

則使用循環中的新xmlhttp。這工作。謝謝 – user2442193

相關問題