1

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
}

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

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

sin
  • 11
  • 1
  • 2

2 Answers2

0

Could your web-server be returning one of the other 'success' status codes from the HTTP specification?

Try testing if (XMLHttp.status >= 200 && XMLHttp.status < 300).

Can you tell us whether you know or don't know if execution-flow is getting to the fill(targetID,data) line? It would be very unusual if the XMLHttp.status check were interfering in the actual data retrieval step.

From what you've told us, it seems that XMLHttp.readyState must be reaching 4 without XMLHttp.status == 200. Have I misinterpreted your question?

Jsnydr
  • 70
  • 10
0

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!");
    }

its work for me or use jquery or prototype.

chandoo
  • 1,276
  • 2
  • 21
  • 32