danwoods
danwoods

Reputation: 4907

basic xmlHttp question

I 'm having some trouble with my javascript code calling my php. Does anyone see an error in the following code? I swear I'm using code just like this on another part of the site...

var xmlHttp = createXmlHttpRequestObject();
var favSongArray = [];

function createXmlHttpRequestObject(){
  var xmlHttp;

  try{
    xmlHttp = new XMLHttpRequest();
  }
  catch(e){
   var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                "MSXML2.XMLHTTP.5.0",
                                "MSXML2.XMLHTTP.4.0",
                                "MSXML2.XMLHTTP.3.0",
                                "MSXML2.XMLHTTP",
                                "Microsoft.XMLHTTP");

  for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++){
    try{
      xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
    }
    catch(e){}
  }
}

if(!xmlHttp){
  alert("Error creating the XMLHttpRequest object.");
}
else{
  return xmlHttp;
}
}


function process(){

  if(xmlHttp){
    alert("sever is available");
    //if yes try
    try{

      xmlHttp.open("GET", "php/getUntimed.php", true);
      xmlHttp.onreadystatechange = function(){handleRequestStateChange();};
      alert("attempted to call p_handleRequestStateChange_test");
      xmlHttp.send(null);
    }//end try
    catch(e){
      alert("Can't connect to server: \n" + e.toString());
    }//end catch
  }//end if xmlHHttp

}//end function

function handleRequestStateChange(){
  if(xmlHttp.readyState == 4){

  if(xmlHttp.status == 200){
   try{
     u_handleServerResponse();
   }//end try
   catch(e){
     alert("Error reading the response: " +e.toString());
   }//end catch
 }//end if
 else{
   alert("There was a problem retriving the data:\n" + xmlHttp.statusText);
 }//end else
 }//end if
 }//end function

 function u_handleServerResponse(){
 //need to clear array each time
 var response = xmlHttp.responseText;

favSongArray = response.split("+");
alert("made it here");
//getFlashMovie("trackTimer").trackTimer(favSongArray[0]);
}

process() is called from an onSubmit trigger. I keep getting a xmlHttp.status of zero. Does that make sense to anyone? Thanks

Upvotes: 0

Views: 360

Answers (4)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

As Jonathan Lonowski says, status == 0 means aborted, and you said you execute that script onsubmit which would trigger the form to submit, thus reload the page and aborting the Ajax request. Take a look here too.

Upvotes: 1

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123463

status == 0 usually means it was aborted -- either by pressing ESC or by changing the current address.

Or, since you're using a global xmlHttp, you may be calling open and/or send before the last request has had time to finish. Not entirely sure which, but one of them starts by calling abort.

Upvotes: 3

ChaosPandion
ChaosPandion

Reputation: 78262

Just navigate here.

http://docs.jquery.com/Ajax

Simple Example:

$.get('MyUrl.aspx', 'MyId=' + id, function(data){
    $(data).appendTo($('#MyDiv'));
});

Upvotes: 1

Sorantis
Sorantis

Reputation: 14722

Why don't you try using ajax frameworks? Like jQuery for example.

Upvotes: 0

Related Questions