I am encountering an issue with my Ajax code as it always returns 0 when I access 'readyState'. I have not been able to identify the source of the problem yet. Any assistance on this matter would be greatly appreciated:
var xhr = null;
function performAjax(inputUrl){
// initialize the XMLHttpRequest object
try{
xhr = new XMLHttpRequest();
alert("XMLHttpRequest");
}
catch(e){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// handle compatibility for older browsers
if( xhr == null ) {
alert("Your browser does not support Ajax");
return;
}
// retrieve the URL
var url = inputUrl;
alert(inputUrl);
// obtain the Ajax response
xhr.onreadystatechange = handler();
//alert(xhr.readyState);
xhr.open("POST", url, true);
xhr.send(null);
}
function handler() {
alert("Handler: " + xhr.readyState + " Status: " + xhr.status);
// only process loaded requests
if(xhr.readyState == 4) { // state 4 indicates data has been received
alert("here");
if(xhr.status == 200) {
alert(xhr.reponseText);
}
else alert("An error occurred with Ajax");
}
}