(function(){ //创建XMLHTTPRequest对象 function CreateXHR(){ if(window.XMLHttpRequest) return new XMLHttpRequest(); else if(window.ActiveXObject){ var aVersions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0"]; for(var i=0;i<aVersions.length;i++){ try{ var XHR = new ActiveXObject(aVersions[i]); return XHR; }catch(e){} } throw new Error("XHR can not be created"); } } var XHR = CreateXHR();//设置请求对象 function callback(){ if(XHR.readyState==4){ if(XHR.status==200){ [b]alert("run")//不运行[/b] var message =newChilddocument.createElement("div"); message.innerHTML=XHR.responseText; document.appendChild(message); } } } function submit(url,params,method){ XHR.onreadystatechange = callback;//设置回调函数 if(!method) method="get" XHR.open(method,url,true); XHR.send(params); } document.getElementById("submit").onclick=submit; })()
帮我看一下为什么alert("run")不能运行
1、你先确定后台对应的方法是否有执行
2、在callback方法中先判断XHR是否存在,然后再看下XHR.status的状态是多少
使用ie8的debug跟一下,即可