日期:2014-05-16  浏览次数:23229 次

AJAX在执行时readyState的状态总是为1 - Web 开发 / Ajax
新手,初学JS和ajax,照着学习视频上的代码写了然后组合了下,但是测的readyState的状态总是为1,不会改变,然后也就得不到服务器端的返回值,帮我看下代码有没有问题,顺便问问是不是php,apache配置也有可能造成这种情况

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function createAjax(){
var request=false;

//window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE8,IE9)
if(window.XMLHttpRequest){
request=new XMLHttpRequest();

if(request.overrideMimeType){
request.overrideMimeType("text/xml");
}
}

//window对象中有ActiveXObject属性存在就是IE
else if(window.ActiveXObject){

var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];

for(var i=0; i<versions.length; i++){
try{
request=new ActiveXObject(versions[i]);

if(request){
return request;
}
}
catch(e){
request=false;
}
}
}

//其他浏览器
else {alert("请使用其他支持AJAX的浏览器")
}

return request;
}
var ajax=null;

function funphp(url) {
  request=createAjax();
request.onreadystatechangge = byphp();
request.open("GET","test2.php?id="+url+"&"+Math.random(),true);
  request.send(null);
}


function byphp(){
  if(request.readyState<4){
  document.getElementById('php100').innerHTML='loading...';
  }
  if(request.readyState==4){
  if(request.status==200){
var byphp100=request.reponseText;
document.getElementById('php100').innerHTML=byphp100;
}
  else {document.getElementById('php100').innerHTML='load...';}
  }

}
//z注释的是自己写的测试的
function aaa(id){
window.location.href='test2.php?id='+id;
}
</script>
</head>
<body>

<a href="#" onclick="funphp('x');">x</a>
<a href="#" onclick="funphp('y');">y</a>
<a href="#" onclick="aaa('z');">z</a>

<p id="php100"></p>

</body>
</html>










------解决方案--------------------
request.onreadystatechangge = byphp(); 
应该改成
request.onreadystatechange = byphp; 

byphp就是回调函数
onreadystatechangge 拼写错误
------解决方案--------------------
onreadystatechangge