DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> Ajax獲取xml信息
Ajax獲取xml信息
編輯:AJAX詳解      日期:2016/12/30 17:53: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);
           }
   }

 

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved