onreadystatechange函数永远不会被调用 [英] onreadystatechange function never gets called
问题描述
好吧...所以我的代码很简单.唯一的问题是,永远不会执行被称为onreadystatechange的函数.我放入一个警报以显示readyState和xmlhttp的状态,分别将其显示为1和0.我不明白为什么状态没有改变.我也确实知道其他一切都正常.我在警报框中显示了我从表单中获取的用户名...它正确显示了该用户名.请在这里帮助我....我只是无法解决这个问题...
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);
}
}
推荐答案
您需要切换 xmlhttp.onreadystatechange
和 xmlhttp.open
的调用顺序,以确保<在打开之前先注册code> onreadystatechange 回调.
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);
这篇关于onreadystatechange函数永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!