在ajax请求中获取0状态

时间:2013-04-17 18:16:45

标签: ajax status

我是ajax的新手。我刚开始学习它。下面是我的第一个ajax脚本。在教程中,相同的脚本显示状态200,但是当我在浏览器上运行时,状态为0.此外,输出仅显示在firefox,chrome和IE中,不显示输出

window.onload=onload_ajax;
var xhr=false;
function onload_ajax() 
{
if(XMLHttpRequest())
    xhr= new XMLHttpRequest();
else if(window.ActiveXObject){
    xhr= new ActiveXObject("Microsoft.XMLHTTP");
    }
if(xhr){
    xhr.onreadystatechange=showState;
    xhr.open("GET","colors.xml",true);
    xhr.send(null);
    }
    else document.getElementById('updateArea').innerHTML="Ahh... Some Error..!!";

}

function showState(){

    var curmsg= document.getElementById('updateArea').innerHTML;
    document.getElementById('updateArea').innerHTML=curmsg+"<p> The current
     Requestst state is "+ xhr.readyState + " and the status is "+ 
     xhr.status + " . </p>"

    }

在firefox上输出

 The current Request state is 1 and the status is 0 .

The current Request state is 1 and the status is 0 .

The current Request state is 2 and the status is 0 .

The current Request state is 3 and the status is 0 .

The current Request state is 4 and the status is 0 . 

1 个答案:

答案 0 :(得分:0)

尝试将浏览器升级到最新版本,然后移除()

if(XMLHttpRequest)