我正在開發一個JavaScript代碼以使用ANT Galio瀏覽器在嵌入式設備上運行。XmlHttpRequest返回狀態4太快
理想情況下,我想讓代碼向另一臺服務器發出獲取請求。在獲取請求之後,該頁面將不允許用戶提交另一個獲取請求,直到從前一個獲取請求收到響應爲止。
出於某種原因,有時候我幾乎立刻就會收到4個readyState。就好像它正在評估以前的XmlHttpRequest對象而不是新的對象。我究竟做錯了什麼?
<script type="text/javascript">
var fail= function (env, resp, stat) {
alert(resp);
};
var succ= function (env, resp) {
};
var canScan = true;
/* start scan */
function scan (name) {
if (canScan) {
//deactivate button
deactivateScanButtons();
//let server know
ajax = new XMLHttpRequest();
var scanUrl = 'http://19X.1XX.X.XX:8080/scan/' + name
ajax.open('GET', scanUrl, true);
ajax.onreadystatechange = function() {
if (ajax.readyState==4) {
//allow button to work again
activateScanButtons();
alert("ready state 4");
};
};
ajax.send();
//initiate scan
xrxScanInitiateScan(
'http://127.0.0.1',
"ftp.xst",
false,
succ,
fail);
}
}
function deactivateScanButtons() {
// canScan = false;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "hidden";
}
function activateScanButtons() {
// canScan = true;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "visible";
}
</script>
這工作得很好。謝謝。 – SLY 2011-04-13 11:13:37