问题
Ok...so my code is very simple. The only problem is that the function to be called onreadystatechange is never getting executed. I put in an alert to display the readyState and the status of xmlhttp which displayed it as 1 and 0 respectively. I cannot understand why the state is not changing. Also i do know for sure that everything else works fine. I put in alert boxes to display the username that i'm taking from the form...it displays it properly. Please help me out here....i just cannot figure this out...
function checkAvailability() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp) {
var regform = document.getElementById("regform");
var username = regform.username.value;
xmlhttp.open("POST", "http://localhost:8080/UsernameAvailability", true);
xmlhttp.onreadystatechange = function() {
alert(xyz);
}
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("username=" + username.value);
}
}
回答1:
You need switch the calling order of xmlhttp.onreadystatechange
and xmlhttp.open
to make sure the onreadystatechange
callback is registered before opening.
xmlhttp.onreadystatechange = function() {
alert(xyz);
};
xmlhttp.open("POST", "http://localhost:8080/UsernameAvailability", true);
来源:https://stackoverflow.com/questions/15587947/onreadystatechange-function-never-gets-called