XMLHttpRequest条件XMLHttp.status == 200的问题

时间:2011-06-06 22:00:40

标签: ajax xmlhttprequest status readystate

我的Ajax工作正常,直到我添加条件状态属性 这是片段

if (XMLHttp.readyState==4 && XMLHttp.status==200){
    // do something
}

这是完整的代码

function getXMLHttp()
            {
             try
             {
              var xmlhttp = new XMLHttpRequest();
              // document.getElementById("Content").innerHTML="<h1>Using XMLHttpRequest Object</h1>";
              //alert('Mozilla XMLHttpRequest Obeject Created Successfully');
             }
             catch(err1)
             {
              var ieXmlHttpVersions = new Array();
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
              ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

              var i;
              for (i=0; i < ieXmlHttpVersions.length; i++)
              {
               try
               {
                var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
                // var catatan = "<h1>Using " + ieXmlHttpVersions[i] + "</h1>";
                break;
               }
               catch (err2)
               {
                var xmlhttp = null;
                //alert(ieXmlHttpVersions[i] + " not supported.");
               }
              }
             }

             if (typeof xmlhttp == "undefined" || xmlhttp == null){
              //document.getElementById("Content").innerHTML="<h1>XMLHttp cannot be created!</h1>";
              alert('XMLHttp Request Object Is not Supported Somehow');
              }
              return xmlhttp;
            }
            var XMLHttp = getXMLHttp();


            function loadData(url, targetID){
                if(!url) {var url="data.txt";}
                if(!targetID){var targetID='ajaxID';}
                XMLHttp.onreadystatechange = function (){getResponse(targetID)};
                XMLHttp.open('GET', url, true);
                XMLHttp.send(null);
            }
            function getResponse(targetID){
                var data = XMLHttp.responseText;
                var ajaxContent=document.getElementById('ajax_content');

                if(XMLHttp.readyState == 4){ 
                // This works Just fine, data from data.txt actually fetched
                // BUT When i Add this if statement with " && XMLHttp.status==200" It's not returning data from data.txt
                    if(data.length > 0){
                        fill(targetID,data);
                    }
                }
            }

            function fill(ID,data){
                hideLoading();
                document.getElementById(ID).innerHTML = data;
            }
            function showLoading(){
                document.getElementById('loading').style.visibility='';
                document.getElementById('loading_text').innerHTML = '....Loading Please Wait....';
            }
            function hideLoading(){
                document.getElementById('loading').style.visibility = 'hidden';
                document.getElementById('loading_text').innerHTML = '';
            }

我的问题是,当我添加data.txt语句时,我无法从&& XMLHttp.status==200获取数据?

2 个答案:

答案 0 :(得分:0)

您的网络服务器是否可以从HTTP specification?

返回其他“成功”状态代码之一

尝试测试(XMLHttp.status >= 200 && XMLHttp.status < 300)

您能否告诉我们您是否知道执行流程是否已到达fill(targetID,data)行?如果XMLHttp.status检查干扰了实际的数据检索步骤,那将是非常不寻常的。

根据您告诉我们的情况,似乎XMLHttp.readyState必须在没有4的情况下达到XMLHttp.status == 200。我误解了你的问题吗?

答案 1 :(得分:0)

我认为它是创建XMLHTTP对象的问题。尝试使用w3schools提供的基本语法。

            var xmlhttp;
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
    alert("Your browser does not support XMLHTTP!");
    }

它为我工作或使用jquery或原型。