javascript发送get、post http请求(转)

时间:2014-4-25    作者:lishalongfa    分类: javascript


1. 获得XMLHttpRequest对象

[javascript] view plaincopy
  1. function createXMLHttpRequest() {  
  2.     var xmlHttp;  
  3.     if (window.XMLHttpRequest) {  
  4.         xmlHttp = new XMLHttpRequest();  
  5.         if (xmlHttp.overrideMimeType)  
  6.             xmlHttp.overrideMimeType('text/xml');  
  7.     } else if (window.ActiveXObject) {  
  8.         try {  
  9.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
  10.         } catch (e) {  
  11.             try {  
  12.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  13.             } catch (e) {  
  14.             }  
  15.         }  
  16.     }  
  17.     return xmlHttp;  
  18. }  



2. 发送get请求

[javascript] view plaincopy
  1. xmlHttp = createXMLHttpRequest();  
  2. var url = "getfiledetail.jsp?fileid="+id;  
  3. xmlHttp.open("GET", url, true);// 异步处理返回   
  4. xmlHttp.onreadystatechange = callback;   
  5. xmlHttp.setRequestHeader("Content-Type",  
  6.         "application/x-www-form-urlencoded;");  
  7. xmlHttp.send();  


3. 发送post请求

[javascript] view plaincopy
  1. var url = "getNginxStatus";  
  2. xmlHttp.open("POST", url, true);  
  3. xmlHttp.onreadystatechange = getStatusBack;  
  4. xmlHttp.setRequestHeader("Content-Type",  
  5. "application/x-www-form-urlencoded;");  
  6. xmlHttp.send(xml);  

WRITTEN BY

avatar