日期:2014-05-20  浏览次数:20801 次

为什么我的 XMLHttpReq.readyState 总是返回1
不知道为什么XMLHttpReq.readyState 总是等于1 
我想实现这样一个功能:
进入页面时,页面先加载一个javascript函数,自动调用一个struts 里面的action连接数据库 拿到数据库里面的值
并把值返回页面填充到下拉列表中.
下面是我的相关代码,不知道那里出错了,我也有一个不解的地方,我在返回那个页面以后那个页面还是重新加载,还是在去调用javascript函数,是不是死循环了?
希望大家帮我看一下!应该怎么解决?谢谢
 <script type="text/javascript"> 
  
  var XMLHttpReq;
  var targetSeId;
  function createXMLHttpRequest(){  
  if(window.XMLHttpRequest) {//Mozilla 
  XMLHttpReq=new XMLHttpRequest();  
  }else if(window.ActiveXObject){//IE 浏览器 
  try { 
  XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP "); 
   
  } catch (e){ 
  try{ 
  XMLHttpReq=new ActiveXObject("Micrsost.XMLHTTP ");
  }catch(e){} 
  } 
  }  
  if(!XMLHttpReq){
  alert("无法创建 XMLHttpRequest 对象!");
  } 
  }
  function loadfirst(){  
  alert("开始加载...");
  var url="http://localhost:8080/IsfotMycar1/loadBrand.do";  
  createXMLHttpRequest();  
  if(XMLHttpReq){
  XMLHttpReq.open("GET",url,true);
  alert("open");
  XMLHttpReq.onreadystatechange=loadcallback();
  XMLHttpReq.send(null);
  }
  }  
  function loadcallback(){
  alert("回调..");
  alert(XMLHttpReq.readyState);
  if(XMLHttpReq.readyState==4){
  if(XMLHttpReq.status==200){
  parseMessage2();
  }else{
  alert("返回不正常"+XMLHttpReq.statusText);
  }
  }
  }
  function parseMessage2(){
  var xmlDoc=XMLHttpReq.responseXML.documentElement;  
  var xSel=xmlDoc.getElementsByTagName('select');  
  var select_root=document.getElementById('select1');
  select_root.options.length=1;
  for(var i=0;i<xSel.length;i++){
  var xValue=xSel[i].childNodes[0].firstChild.nodeValue;  
  var xText=xSel[i].childNodes[1].firstChild.nodeValue;  
  select_root.options[i+1]=new Option(xText,xValue);
  }
  }
 </script>

<body onload="loadfirst()">
  <form action="/inquireScar.do" method="post">
  请选择品牌:<select id="select1" >
  <option value="">请选择--</option>
  </select>
</form>
</body>


=============================================================================
action 类:

public class LoadBrandAction extends Action {
/*
* Generated Methods
*/

/** 
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
private SecCarService secCarServiceImpl;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
List list=secCarServiceImpl.getAllCar();
System.out.println("list的值:"+list);
if(list!=null){
StringBuffer xml=new StringBuffer("<selects>");