Javascript XMLHTTPREQUEST ONREADY状态更改错误
有谁能帮助或指导我找出为什么下面的代码不能按预期运行(即警告来自php的回音),而是警告“发生了一些负面的事情” 我的Javascript代码如下所示:Javascript XMLHTTPREQUEST ONREADY状态更改错误,javascript,php,xmlhttprequest,Javascript,Php,Xmlhttprequest,有谁能帮助或指导我找出为什么下面的代码不能按预期运行(即警告来自php的回音),而是警告“发生了一些负面的事情” 我的Javascript代码如下所示: window.onload = function(){ var regForm = document.getElementById("register").onsubmit = checkForm; } //Let's see if this user already exists function checkForm(){
window.onload = function(){
var regForm = document.getElementById("register").onsubmit = checkForm;
}
//Let's see if this user already exists
function checkForm(){
alert("You pressed Register!");
var cc = document.getElementById("un3").value;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
alert("You are using XML HTTP REQUEST");
}else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
alert("YOU ARE USING ACTIVE X OBJECT");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
return false;
}else{
alert("Something negative happened");
}
}
xmlhttp.open("GET","user_exists.php?q="+cc,true);
xmlhttp.send();
}
简化我的php文件以使其正常工作:
<?php
$q=$_GET["q"];
echo "something good happened";
?>
这是因为
onreadystatechange
也会发生变化
试试这个:
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4) {
// only check when state is "the communication is done"
if (&& xmlhttp.status==200){
alert(changes when connecting before the result is therexmlhttp.responseText);
return false;
}else{
alert("Something negative happened");
} else {
// ignore other states
}
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
alert(xmlhttp.responseText);
return false;
}else{
alert("Something negative happened");
}
}
}
不过,更好的方法是使用一个像样的JavaScript库,如jQuery或dojo,这样您就不必自己完成所有这些工作。试试以下方法:
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4) {
// only check when state is "the communication is done"
if (&& xmlhttp.status==200){
alert(changes when connecting before the result is therexmlhttp.responseText);
return false;
}else{
alert("Something negative happened");
} else {
// ignore other states
}
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
alert(xmlhttp.responseText);
return false;
}else{
alert("Something negative happened");
}
}
}
readyState出现了好几次,但并不总是有4次。
看看
我更喜欢将响应从请求中去掉,以保持代码的整洁。在您的情况下,这将是:
xmlhttp.onreadystatechange=responshandler;
function responsehandler(){
//here the response code
}
确保声明
var-xmlhttp代码>在函数外部使其全局化。是的,我感觉连接在结果出现之前就已经发生了。不幸的是,我尝试了你的建议,但仍然不起作用(尝试将响应状态记录到Javascript控制台,这样您就可以看到它实际上完成了所有步骤。Javascript没有显示任何错误,当我提醒xmlhttp.response时,它显示我的php文件中的$q没有索引/未定义。不确定为什么会发生这种情况。我自己检查了我的php文件,它可以工作,但与java结合在一起脚本;无。进一步澄清一下,如果我删除$q,那么响应/提醒的是“发生了一些好事”。问题似乎是从我的javascript接收$q,然后将其发送到mysql数据中以获得结果。不用说,没有javascript,php文件可以自己工作。@Tez是那里的值cc
?另外,您可以使用Firebug查看“引擎盖下”发生了什么。感谢您的努力,但它对我仍然不起作用。将警报(xmlhttp.readyState)
置于if(xmlhttp.readyState==4)之前
。它达到4了吗?不,它没有达到4,是的,user\u exists是正确的。我很困惑,因为我有另一段代码做了类似的事情,并且使用了相同的原理。我只能假设它与表单有关,或者我做了一些我还没有注意到的非常愚蠢的事情。啊,以前有过。Check如果你的所有div都在表单中关闭了。否则它不会发布。问题是$q没有在我的php文件中定义。因此,出于某种原因,我的javascript没有连接到php文件以发送信息来定义$q(我想)…我不知道如何使这件事起作用!哈哈