原生js版ajax请求

    function getXMLHttpRequest() {  
              var xhr;  
              if(window.ActiveXObject) {  
                       xhr= new ActiveXObject("Microsoft.XMLHTTP");  
              }else if (window.XMLHttpRequest) {  
                       xhr= new XMLHttpRequest();  
              }else {  
                       xhr= null;  
              }  
              return xhr;  
    }  
      
    function save() {  
              var xhr = getXMLHttpRequest();  
              xhr.open("post","url");  
              var data = "name=mikan&address=street...";  
              xhr.send(data);  
              xhr.onreadystatechange= function() {  
                       if(xhr.readyState == 4 && xhr.status == 200) {  
                                alert("returned:"+ xhr.responseText);  
                       }  
              };  
    } 

 

posted @ 2015-12-04 10:38  小小高  阅读(10334)  评论(0编辑  收藏  举报