Home > Backend Development > PHP Tutorial > 关与ajax的有关问题

关与ajax的有关问题

WBOY
Release: 2016-06-13 09:59:02
Original
767 people have browsed it

关与ajax的问题
var xmlHttp;
function S_xmlhttprequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();

}
}

function myajax(url){

S_xmlhttprequest();
xmlHttp.open("GET","for.php?id="+url,true);
xmlHttp.onreadystatechange=byphp();
xmlHttp.send(null);
}

function byphp(){

var byphp1=xmlHttp.responseText;

document.getElementById("php").innerHTML="test";
}

前面的两个函数都是对的。最后一个函数有问题。我把var byphp1=xmlHttp.responseText这句注释后最后前台可以显示test,但是加上var byphp1=xmlHttp.responseText后,就显示网页有错误。


for.php的内容是

if($id=$_GET['id']){
for($i=1;$i echo $id;
}
exit();
}
?>


------解决方案--------------------
你代码写错了?记得结贴!发了十几分钟找到错误!给你写个完整的,你的for.php页不变

PHP code
<script language="javascript">var xmlHttp;function S_xmlhttprequest(){if(window.ActiveXObject){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}else if(window.XMLHttpRequest){xmlHttp=new XMLHttpRequest();}}function myajax(url){S_xmlhttprequest();xmlHttp.open("GET","for.php?id="+url,true);xmlHttp.onreadystatechange=byphp;//这里不要加byphp()xmlHttp.send(null);}function byphp(){var byphp1= xmlHttp.responseText;document.getElementById("php").innerHTML=byphp1;}</script><div id="php">测试用的</div><div onclick="myajax('www.163.com')">点击触发</div><!--怀疑你这里也调用错了,你该不会去调用byphp了吧--><br><font color="#e78608">------解决方案--------------------</font><br>装火狐浏览器+firebug,可调试js哪里有错误。<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template