newleague 发表于 2013-1-29 11:52:56

ajax 回调 多浏览器

var xmlHttp;
  
     function getxml(){
      if(window.XMLHttpRequest) {
          xmlHttp = new XMLHttpRequest();
          if (xmlHttp.overrideMimeType) {
              xmlHttp.overrideMimeType("text/xml");
          }
      }
      else if (window.ActiveXObject) {
          try {
              xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
              try {
                  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
          }
      }
      if (!xmlHttp) {
          window.alert("can't create XMLHttpRequest object.");
          return null;
      }
      return xmlHttp;
      }
  
  
  function getModel(){
   xmlHttp=getxml();
   var makeName = document.getElementById("makeName").value;
   var yearName = document.getElementById("yearName").value;
   var url = "<%=request.getContextPath()%>/website/index.do?method=getModel&makeName="+makeName+"&yearName="+yearName;
   xmlHttp.open("POST",url,false);
   xmlHttp.send(null);
   xmlHttp.onreadystatechange = callback();
   
  }
  
  function callback(){
   if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
     getModelResponse();
     xmlHttp = null;
    }
   }
  }
  
  function getModelResponse(){
   clearModelList();
   var modelNames = xmlHttp.responseXML.getElementsByTagName("modelName");
   
   var dlcModel = document.getElementById("modelName");
   var option = null;
   for(var i=0;i<modelNames.length;i++){
    option = document.createElement("option");
    option.appendChild(document.createTextNode(modelNames.firstChild.nodeValue));
    option.value = modelNames.firstChild.nodeValue;
    dlcModel.appendChild(option);
   }
  }
 
   function clearModelList(){
    var model = document.getElementById("modelName");
    while(model.childNodes.length>0){
     model.options.length = 0
    }
   }
页: [1]
查看完整版本: ajax 回调 多浏览器