Here’s my script:
var Ajax = {
init: function(){
try{
this.xmlHttp = new XMLHttpRequest();
}
catch( e ){
try{
this.xmlHttp = new ActiveXObject( "Microsoft.XMLHttp" );
}
catch( e ){
}
}
return this;
},
get: function( url , async , fn ){
this.xmlHttp.open( "GET" , url , async );
console.log( "1" );
this.xmlHttp.setRequestHeader( 'Content-Type' , 'application/x-www-form-urlencoded' );
console.log( this.xmlHttp.readyState );
this.xmlHttp.onreadystatechange = function(){
console.log( "3" );
if( this.xmlHttp.readyState == 4 ){
if( this.xmlHttp.status == 200 ){
try{
if( fn ) return fn( this.xmlHttp.responseText );
}
catch( e ){
alert( e );
}
}
else alert( "bad status" );
}
else alert( "bad state" );
}
}
}
And the line to call it:
Ajax.init().get( "localhost/engine.php" , true , function( txt ){alert(txt)});
Now, at the console I get this:
1
1
Hence, I understand the running stucks at this.xmlHttp.onreadystatechange = function(){ while the readyState is 1. Could you, please, tell me, what’s wrong here? Am I missing some bigger error here?
After researching a bit, it seems like the xmlHttp does not really open the url (no request is seen in Chrome’s console and Firebug)…
You are never actually starting the request! Add this at the end of your get() function:
Hope this helps