与XMLHtt prequest条件XMLHttp.status == 200问题条件、问题、prequest、XMLHtt

由网友(罂粟的毒°难解)分享简介:我的Ajax的工作原理就好了,直到我添加了一个有条件的状态属性。这里的段My Ajax works just fine until I add a conditional status property.Here's the snippetif (XMLHttp.readyState==4 && XMLHttp...

我的Ajax的工作原理就好了,直到我添加了一个有条件的状态属性。 这里的段

My Ajax works just fine until I add a conditional status property. Here's the snippet

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

和这里的完整code

and here's the complete code

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 语句?

My question is why I can't get the data from data.txt when I add && XMLHttp.status==200 statement ?

推荐答案

我认为它在创建XMLHTTP对象的问题。尽量使用已通过W3Schools的提供的基本语法。

I think its the problem in creating the XMLHTTP object. try use the basic syntax that has been provided by 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或原型的工作。

its work for me or use jquery or prototype.

阅读全文

相关推荐

最新文章