gpt4 book ai didi

javascript - onreadystatechange 函数永远不会被调用

转载 作者:行者123 更新时间:2023-11-30 10:33:16 27 4
gpt4 key购买 nike

好的...所以我的代码非常简单。唯一的问题是调用 onreadystatechange 的函数永远不会被执行。我发出警报以显示 readyState 和 xmlhttp 的状态,分别显示为 1 和 0。我不明白为什么状态没有改变。我也确实知道其他一切都正常。我放入警告框以显示我从表单中获取的用户名...它正确显示。请帮帮我……我就是想不通……

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.onreadystatechangexmlhttp.open的调用顺序,确保onreadystatechange回调在打开前注册。

xmlhttp.onreadystatechange = function() {
alert(xyz);
};
xmlhttp.open("POST", "http://localhost:8080/UsernameAvailability", true);

关于javascript - onreadystatechange 函数永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15587947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com