原生xhr對象存在較多的兼容性,IE6及以前版本使用ActiveXObject對象來建立,IE7之後使用兼容版本的MSXML2.XMLHttp、MSXML2.XMLHttp3.0、MSXML2.XMLHttp6.0.php
ie7以前版本git
1 function createXHR() { 2 if (typeof arguments.callee.activeXString !== 'string') { 3 var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp']; 4 for (var i = 0,len = versions.length;i < len;i++) { 5 try { 6 var xhr = new ActiveXOject(versions[i]); 7 arguments.callee.activeXString = versions[i]; 8 } catch (ex) { 9 10 } 11 } 12 } 13 return new ActiveXOject(arguments.callee.activeXString); 14 }
ie七、FIreFox、Opera、Chrome及Safari都支持之原生XHR對象github
1 function createXHR() { 2 if (typeof XMLHttpRequest !== 'undefined') { 3 return new XMLHttpRequest(); 4 } else if (typeof ActiveXObject !== 'undefined') { 5 if (typeof arguments.callee.activeXString !== 'string') { 6 var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp']; 7 for (var i = 0,len = versions.length;i < len;i++) { 8 try { 9 var xhr = new ActiveXOject(versions[i]); 10 arguments.callee.activeXString = versions[i]; 11 return xhr; 12 } catch (ex) { 13 continue; 14 } 15 } 16 } 17 return new ActiveXObject(arguments.callee.activeXString); 18 } else { 19 throw new Error("no xhr object available"); 20 } 21 }
XHR對象用法數據庫
一、調用open,接受三個參數:請求類型,請求連接,請求是否異步瀏覽器
xhr.open('get', 'test.action', false);服務器
二、調用send,發送特定數據,若是沒有特定數據,也必須傳入null,某些瀏覽器是會檢測改參數併發
xhr.send(null)app
三、服務器響應後,響應後的數據會自動填充XHR對象的屬性異步
responseText:響應主體被返回的文本函數
responseXML:若是響應內容類型是'text/xml'或'application/xml',這個屬性中將包含着響應數據的XML DOM文檔
status: 響應的HTTP狀態
statusText: http狀態說明
咱們通常使用是status狀態碼來檢測狀態。
1 xhr.onreadystatechange = function() { 2 if (xhr.readyState == 4) { 3 if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { 4 console.log(xhr.statusText); 5 } else { 6 console.log('unsuccessful'); 7 } 8 } 9 };
四、檢測readyState屬性
上面沒有檢測onreadystatechange事件,屬於同步請求,咱們大部分仍是須要使用異步請求,因此咱們可使用上面的函數來監聽readyState的每次變化。
0未初始化,還沒有調用open;
1啓動,調用open,還沒有調用send;
2發送,調用send,還沒有接受響應;
3接受,已接受部分數據;
4完成,接受所有數據。
每一次的readyState的變化都會觸發readyStatechange事件,故而咱們能夠用過readystate來檢測不一樣階段。設置onreadystatechange事件必須在open以前。
1 var xhr = new createXHR(); 2 xhr.onreadystatechange = function(){ 3 if(xhr.readyState == 4){ 4 if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ 5 console.log(xhr.statusText); 6 }else{ 7 console.log("Request was unsuccessful:" + xhr.status); 8 } 9 } 10 }; 11 xhr.open("get","example.php",true); 12 xhr.send(null);
http頭部信息
默認狀況瀏覽器在發送XHR請求同時,發送不少頭信息
Accept: 瀏覽器能處理的內容類型
Accept-Charset:瀏覽器可以顯示的字符集
Accept-Encoding:瀏覽器能處理的壓縮編碼
Accept-Language:瀏覽器當前設置的語言
Connection:瀏覽器與服務器之間鏈接的類型
Cookie:當前頁面設置的任何Cookie
Host:發送請求頁面所在的域
Referer:發送請求頁面所在的域
User-Agent:瀏覽器全部代理用戶字符串
一、設置自定義頭信息,setRequestHeader方法,參數爲鍵值對,不過必須在open以後send以前
二、獲取頭部自定義信息,getResponseHeader方法,參數爲字符串。
GET請求
一、get請求參數url的查詢字段必須通過正確的編碼才行。使用encodeURIComponent進行編碼,不能用用encodeURI,這兩編碼範圍不同。
1 function addURLParam (url,name,value) { 2 if (url && name && value) { 3 url += (url.indexOf("?") == -1 ? "?" : "&"); 4 url += encodeURIComponent(name) + "=" + encodeURIComponent(value); 5 } 6 return url; 7 }
二、post請求
默認服務器對post請求和提交表單請求並不會一視同仁,必須用程序來讀取發送過來的原始數據,從中去有用部分。能夠經過設置Content-Type爲application/x-www-form-urlencoded來模擬表單。
1 function submitData(){ 2 var xhr = createXHR(); 3 xhr.onreadystatechange = function (event){ 4 if(xhr.readyState == 4){ 5 if((xhr.status >= 200 && xhr.status < 300)||xhr.status == 304){ 6 alert(xhr.responseText); 7 }else{ 8 alert("Request was unsuccessful:"+ xhr.status); 9 } 10 } 11 }; 12 xhr.open("post","postexample.php",true); 13 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 14 var form = document.getElementById("user-info"); 15 xhr.send(serialize(form)); 16 }
瀏覽器差別
一、超時設置
IE8爲XHR對象添加了一個timeout屬性,表示請求等待響應多少毫秒以後終止。
1 var xhr = createXHR(); 2 xhr.onreadystatechange = function(event){ 3 try{ 4 if(xhr.readyState == 4){ 5 if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ 6 alert(xhr.responseText); 7 }else{ 8 alert("Request was unsuccessful:"+ xhr.status); 9 } 10 } 11 }catch (ex){ 12 //假設由ontimeout事件處理程序處理 13 } 14 }; 15 xhr.open("get","timeout.php",true); 16 xhr.timeout = 1000; 17 xhr.ontimeout = function(){ 18 alert("Request did not not return in a second."); 19 }; 20 xhr.send(null);
還有一些onload、progress事件,能夠經過這兩事件作一些特殊的事情。
附上本身寫的xhr地址:https://github.com/schacker/xhr
附上status各類狀態碼