好吧...所以我的代码很简单.唯一的问题是,永远不会执行被称为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", "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 的调用顺序,以确保 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", "localhost:8080/UsernameAvailability", true);更多推荐
onreadystatechange函数永远不会被调用
发布评论