Heim  >  Artikel  >  Backend-Entwicklung  >  关与ajax的有关问题

关与ajax的有关问题

WBOY
WBOYOriginal
2016-06-13 09:59:02725Durchsuche

关与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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn