Ajax获取xml信息

网络整理 - 07-26

下面是Ajax获取xml的信息:

//定义一个成员变量
var XMLHttpReq;
//获取浏览器的信息
function createXMLHttpRequest(){
       if(window.XMLHttpRequest){ //Mozilla
                 XMLHttpReq=new XMLHttpRequest();
       }else if(window.ActiveXObject){
                 try{
                           XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
                 }catch(e){
                           try{
                                    XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
                          }catch(e){}
                 }
        }
}

//获取状态信息
function proce(textNameInfor,indexNum){
           if(XMLHttpReq.readyState==4){
                      if(XMLHttpReq.status==200){
                                 var res=XMLHttpReq.responseXML.getElementsByTagName("user-factory") [0].firstChild.data;
                                ///alert(XMLHttpReq.responseXML.xml);
                               res=res.replace(/(^\s*)|(\s*$)/g,"");
                              if(res == null||res == "null"||res == ""){
                                          document.getElementById(textNameInfor).value='无此条码!';
                                          document.getElementById(textNameInfor).style.color="red";
                                          document.getElementById(indexNum).value=null;
                              }else{  
                                          document.getElementById(textNameInfor).value='正确!';
                                          document.getElementById(textNameInfor).style.color="black";
                                          document.getElementById(indexNum).value=res;     
                              }
                      }else{
                                          v.value='....';
                      }
           }
}

function send(textName,textNameInfor,urlName,indexNum){  
           var name=document.getElementById(textName).value;
           if(name==""){
                     //textNameInfor.value='必填填,不得为空!';
                     document.getElementById(textNameInfor).value='必填填,不得为空!';
                     document.getElementById(textNameInfor).style.color="red";
                     return false;
           }else{
                     url=urlName+name;
                     createXMLHttpRequest();
                     XMLHttpReq.open("GET",url,true);
                     v=textNameInfor;
                     XMLHttpReq.onreadystatechange=function(){proce(textNameInfor,indexNum)};
                     XMLHttpReq.send(null);
           }
   }