onreadystatechange函数永远不会被调用

编程入门 行业动态 更新时间:2024-10-28 06:29:56
本文介绍了onreadystatechange函数永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧...所以我的代码很简单.唯一的问题是,永远不会执行被称为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函数永远不会被调用

本文发布于:2023-11-08 09:41:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   函数   onreadystatechange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!