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

XMLHttpReq.readyState ==0老出这样的错!那位大哥知道的来看看!谢谢了! - Web 开发 / Ajax
我的js 脚本:

var XMLHttpReq;
  //创建XMLHttpRequest对象  
function createXMLHttpRequest() {

  if (window.XMLHttpRequest) {alert("Mozila") //Mozilla 浏览器
  XMLHttpReq = new XMLHttpRequest();
  alert(XMLHttpReq);
  } else {
  if (window.ActiveXObject) {// IE浏览器
  try {
  XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e) {
  try {
  XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
  }
  }
  }
  }
}
//发送请求函数
function loginRequest(url) {
  createXMLHttpRequest();
XMLHttpReq.onreadystatechange = processLoginResponse();//指定响应函数
  XMLHttpReq.open("GET",url,true);
  XMLHttpReq.send(); // 发送请求
}

// 处理身份验证返回信息函数
function processLoginResponse() {
alert("process");

  if (XMLHttpReq.readyState == 4) { // 判断对象状态
  if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
  var res=XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.nodeValue;
 
  if (res==1){
  window.alert("用户名错误!");  
  }
  else if (res==2){
  window.alert("密码错误!");  
  }
  else if (res==0){
  var name=XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
  window.location = "index.jsp?name="+name;  
  }
  } else { //页面不正常
  window.alert("您所请求的页面有异常。");
  }
  }if(XMLHttpReq.readyState ==0)
  {
  alert("????");
  }
  }
function userCheck() {alert("name");
var name = document.loginForm.name.value;
var psw = document.loginForm.pass.value;

if(name=="") {
window.alert("用户名不能为空。");
document.loginForm.name.focus();
return false;
}
else{
loginRequest("<%=request.getContextPath()%>/login?name=" + name + "&pass=" + psw );
}
}
*****************************************************************************************************



我是IE7的浏览器,它为什么给我生成一个Mozilla 浏览器的对象啊????
单独测试我的servlet没什么问题但用在loginRequest(url)这里请求的话,就不走我的servlet!
XMLHttpReq.readyState ==0老出这样的错?????????
那位大哥懂得来看看!谢谢了!我找了一下午了,还没找出来!

------解决方案--------------------
好象ie7是xmlhttprequest对象,
ie6-的才是activexobject

绑定状态处理函数不对
XMLHttpReq.onreadystatechange = processLoginResponse();//指定响应函数 

==>
XMLHttpReq.onreadystatechange = processLoginResponse;



而且要注意一点,你的XMLHttpReq.status要想返回200,必须通过iis/tomcat来访问,要不直接拖进浏览器返回的是0
------解决方案--------------------
1楼的兄弟真是乐于助人啊!!!!!!
初学者的导师啊!!!
讲解的如此详细,实在令人感动啊!!!! 向之学习!!!