【问题标题】:Problem with XMLHttpRequest conditional XMLHttp.status==200XMLHttpRequest 条件 XMLHttp.status==200 的问题
【发布时间】:2011-09-09 15:41:15
【问题描述】:

在我添加条件状态属性之前,我的 Ajax 工作正常。
这是sn-p

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 = '';
            }

我的问题是为什么我在添加&amp;&amp; XMLHttp.status==200 语句时无法从data.txt 获取数据?

【问题讨论】:

    标签: ajax xmlhttprequest status readystate


    【解决方案1】:

    我认为这是创建 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 或原型。

    【讨论】:

      【解决方案2】:

      您的网络服务器是否会从 HTTP specification? 返回其他“成功”状态代码之一

      尝试测试(XMLHttp.status &gt;= 200 &amp;&amp; XMLHttp.status &lt; 300)

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多