views:

165

answers:

5

Hi all. 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

A: 

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

Sorantis
A: 

Just navigate here.

http://docs.jquery.com/Ajax

Simple Example:

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

I'd still rather see what I'm doing wrong with the code I have then learn a whole new syntax. Not trying to be snobby, just lazy :)

danwoods
It seems like a waste though. The smart developers behind jQuery have already taken care of these annoying issues.
ChaosPandion
If you're talking about the "just use jquery" answers, then I'd say you're not being lazy -- on the contrary, it's more work to understand how something works before using a facade. I had a physics professor who said he didn't "own" a theorem until he could prove it, and I believe the same applies here. As for your problem, make sure to visit the PHP page itself to ensure it's returning what you think.
harpo
This code is a prime candidate for abstraction. Instead of wasting time figuring out the intricate details of XMLHttpRequest he can spend more time creating a great website.
ChaosPandion
Some people like more to hack than just make great websites. I'm one of them actually.
Ionuț G. Stan
I take it all back. JQuery rules! Seems really fast too...
danwoods
There are things way more interesting then executing a successful AJAX request. :)
ChaosPandion
LOL, Another convert! Muhaahahahah!
ChaosPandion
Seriously -- once you move past "how does this work?" it's just infinitely better to use a good framework which solves annoying real-world day-in-day-out issues
DarkSquid
+3  A: 

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.

Jonathan Lonowski
+1  A: 

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.

Ionuț G. Stan
Thanks, I knew the page reloading was messing with it somehow
danwoods