Php Ajax返回随机值?
我正在开发一个简单的AJAX页面。当页面加载时,它应该从PHP页面获取结果并将其显示在文本框中。如果结果是“1”(应该是),那么它应该弹出一个警告,说“准备就绪” 主页的代码(t1_wait.php):Php Ajax返回随机值?,php,javascript,ajax,Php,Javascript,Ajax,我正在开发一个简单的AJAX页面。当页面加载时,它应该从PHP页面获取结果并将其显示在文本框中。如果结果是“1”(应该是),那么它应该弹出一个警告,说“准备就绪” 主页的代码(t1_wait.php): 等待。。。 功能更新(id) { var-xmlhttp; if(window.XMLHttpRequest){ //IE7+、Firefox、Chrome、Opera、Safari的代码 xmlhttp=新的XMLHttpRequest(); }else if(window.ActiveXOb
等待。。。
功能更新(id)
{
var-xmlhttp;
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else if(window.ActiveXObject){
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}否则{
警报(“您的浏览器不支持XMLHTTP!”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4){
if(xmlhttp.responseText==“1”)
警报(“准备就绪!”);
}
document.myForm.status.value=xmlhttp.responseText;
}
}
var requesturl=“t1_checkMatch.php?id=“+id;
open(“GET”,requesturl,true);
xmlhttp.send(空);
//延迟1秒
变量日期=新日期();
var curDate=null;
do{curDate=new Date();}
而(curDate<1000);
}
地位:
调用的PHP页面(t1_checkMatch.PHP)(所有数据库信息都替换为*****):
当我转到t1_wait.php?id=16(通过GET传递id=16的主页)时,它应该向t1_checkMatch.php?id=16发送一个请求,返回(是的,我选中了)1。这将触发一个警告,提示“就绪”,并导致文本框中出现1,但这两种情况都不会发生。文本框为空
怎么了?谢谢 我相信您遇到的问题是由于输入错误造成的 xmlhttp.responceText 真的应该
xmlhttp.responseText
--更新
您似乎还缺少一个{
:
if(xmlhttp.responseText=="1")
alert("Ready!");
}
应该是
if(xmlhttp.responseText=="1"){
alert("Ready!");
}
我相信你遇到的问题是由于打字错误造成的 xmlhttp.responceText 真的应该
xmlhttp.responseText
--更新
您似乎还缺少一个{
:
if(xmlhttp.responseText=="1")
alert("Ready!");
}
应该是
if(xmlhttp.responseText=="1"){
alert("Ready!");
}
您有拼写错误:
if(xmlhttp.responceText=="1")
应该是:
if(xmlhttp.responseText=="1")
(您的回答拼写错误)您有拼写错误:
if(xmlhttp.responceText=="1")
应该是:
if(xmlhttp.responseText=="1")
(您拼写的响应不正确)好的。我知道了,但我不知道我做了什么。我确实有一个打字错误,但这不是问题。PHP代码是相同的,以下是主页代码:
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function update(id){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
if(ajaxRequest.responseText.indexOf("1")!=-1){
document.myForm.status.value = "Ready!";
window.location = "t1_game.php?id="+id;
}else{
document.myForm.status.value = "Waiting..."
update(id);
}
}
}
ajaxRequest.open("GET", "t1_checkMatch.php?id="+id, true);
ajaxRequest.send(null);
}
<?php
echo "update(".$_GET["id"].");"
?>
//-->
</script>
<form name='myForm'>
Status: <input type='text' name='status' />
</form>
</body>
</html>
地位:
好的。我想出来了,但我不知道我做了什么。我确实有一个打字错误,但这不是问题。PHP代码是一样的,下面是主页代码:
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function update(id){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
if(ajaxRequest.responseText.indexOf("1")!=-1){
document.myForm.status.value = "Ready!";
window.location = "t1_game.php?id="+id;
}else{
document.myForm.status.value = "Waiting..."
update(id);
}
}
}
ajaxRequest.open("GET", "t1_checkMatch.php?id="+id, true);
ajaxRequest.send(null);
}
<?php
echo "update(".$_GET["id"].");"
?>
//-->
</script>
<form name='myForm'>
Status: <input type='text' name='status' />
</form>
</body>
</html>
地位:
你为什么又发明了轮子?有了其中一个库(例如mootools),你的整个JS代码最多会减少到1-5行。然后他会为mootools添加几百行:)你为什么又发明轮子?有了其中一个库(例如mootools)你的整个JS代码最多会减少到1-5行。然后他会为mootools添加几百行:)已经解决了,我只是想把它放在那里,以防其他人有同样的问题。它已经解决了,我只是想把它放在那里,以防其他人有同样的问题