XMLHttpRequest
- 創(chuàng)建:
var createXHR = function(){
var xhr = false;
try{
xhr = new XMLHttpRequest();//嘗試直接創(chuàng)建攻礼,適用于IE以外的大多數(shù)瀏覽器
}
catch(e){
try{
xhr = new ActiveXObject('Mxsm112.XMLHTTP');//嘗試使用較新版本IE的創(chuàng)建方式
}
catch(e){
try{
xhr = new ActiveXObject('Microsoft.XMLHTTP');//嘗試使用較老版本IE的創(chuàng)建方式
}
catch(e){
xhr = false;//創(chuàng)建失敗业踢,返回false提示
console.log(e.name + ' : ' + e.message);
}
}
}
return xhr;
}
- 方法:
- open(method,url,async) :
- method指定請(qǐng)求使用的方法,包括GET礁扮、POST知举、HEAD瞬沦、DELETE;
- url即請(qǐng)求地址雇锡;
- async規(guī)定是否啟用異步請(qǐng)求逛钻,異步請(qǐng)求將直接返回,需要使用事件監(jiān)聽(tīng)器在響應(yīng)就緒后動(dòng)態(tài)處理锰提,默認(rèn)為true曙痘;
- send() : 發(fā)送請(qǐng)求并根據(jù)同步異步設(shè)定適時(shí)返回,接受一個(gè)可選的請(qǐng)求體作為參數(shù)(在GET和HEAD中被忽略)立肘;
- abort() : 立即中斷已經(jīng)發(fā)送的請(qǐng)求边坤;對(duì)應(yīng)請(qǐng)求的就緒狀態(tài)被置為0,但不會(huì)觸發(fā)readystatechange事件谅年;
- onreadystatechange() : 當(dāng)readyState發(fā)生變化時(shí)執(zhí)行的事件處理函數(shù)茧痒;
- ontimeout() : 請(qǐng)求超時(shí)后調(diào)用的方法;
- 屬性:
- readyState:
- 0 請(qǐng)求未初始化融蹂,open()未調(diào)用旺订;
- 1 請(qǐng)求已建立,未發(fā)送超燃,send()尚未調(diào)用耸峭;
- 2 請(qǐng)求已發(fā)送,處理中淋纲,可以從響應(yīng)中獲取首部(header)和狀態(tài)(status)劳闹;
- 3 響應(yīng)處理中,響應(yīng)中已有部分?jǐn)?shù)據(jù)可用洽瞬,但未完全就緒本涕;
- 4 響應(yīng)已就緒;
- 注意伙窃,各瀏覽器中對(duì)就緒狀態(tài)的處理并不一致菩颖,不能過(guò)于依賴狀態(tài)來(lái)執(zhí)行操作,測(cè)試在chrome(53)和Firefox(43)中onreadystatechange方法只能在狀態(tài)1和4時(shí)被調(diào)用为障,IE(11)則更奇特晦闰,依次出現(xiàn)了1、1鳍怨、2摄咆、3鬓椭、4的狀態(tài),1出現(xiàn)了兩次。
- 無(wú)法通過(guò)onreadystatechange()僅在就緒狀態(tài)發(fā)生改變時(shí)被調(diào)用副渴,但在readyState被在請(qǐng)求中斷(abort()被調(diào)用)被置0時(shí)不會(huì)觸發(fā)該方法复濒,因而無(wú)法通過(guò)該方法獲取readyState為0的狀態(tài),只能在請(qǐng)求未初始化或被中斷時(shí)手動(dòng)獲取纽疟;
- upload:
- 返回一個(gè)代表上傳過(guò)程的對(duì)象,可以在其上注冊(cè)事件監(jiān)聽(tīng)器以追蹤上傳進(jìn)程憾赁;
- 可選事件監(jiān)聽(tīng)器包括
- onloadstart : the fetch starts (請(qǐng)求開(kāi)始裝載數(shù)據(jù)時(shí)調(diào)用的方法)
- onprogress : data transfer is going on (周期性調(diào)用的反饋上傳進(jìn)度的方法)
- onabort : the fetch operation was aborted (請(qǐng)求中斷后調(diào)用的方法)
- onerror : the fetch failed (請(qǐng)求中出現(xiàn)錯(cuò)誤時(shí)調(diào)用的方法)
- onload : the fetch succeeded (請(qǐng)求在抓取內(nèi)容成功后返回時(shí)調(diào)用的方法)
- ontimeout : the fetch operation didn't complete by the timeout the author specified (請(qǐng)求超時(shí)調(diào)用的方法)
- onloadend : the fetch operation completed (either failed or succeeded)
- response:
- 返回相應(yīng)的實(shí)體主體(response entity body)污朽,具體類型取決于responseType的值,在1.0中只有DOMString和 Document龙考,在2.0中擴(kuò)充了Blob蟆肆、FormData、ArrayBuffer洲愤、File等颓芭;
- 當(dāng)responseType為‘text’或空,response在請(qǐng)求處于loading狀態(tài)時(shí)保存部分響應(yīng)文本(response text)顷锰;
- responseType: 返回一個(gè)定義響應(yīng)實(shí)體類型的枚舉值
- responseText: 返回一個(gè)包含響應(yīng)文本(response as text)的DOMString柬赐,或者null;
- responseXML: 返回一個(gè)包含響應(yīng)的Document官紫,或者null(請(qǐng)求未發(fā)送肛宋,響應(yīng)無(wú)法解析);
- status: 返回響應(yīng)的狀態(tài)碼束世;
- statusText: 返回響應(yīng)的狀態(tài)碼及服務(wù)器的反饋信息酝陈;
- timeout: 定義請(qǐng)求在被終結(jié)前可以維持的最大時(shí)長(zhǎng),以毫秒為單位毁涉;
- withCredentials: 一個(gè)布爾值沉帮,指示了Cross Site Access Control請(qǐng)求是否需要使用證書(shū)(如cookies,授權(quán)首部(authorization headers)或者TLS用戶證書(shū)等)贫堰;該值的設(shè)定對(duì)同域請(qǐng)求沒(méi)有影響穆壕;
- In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is false. XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request. The third-party cookies obtained by setting withCredentials to true will still honor same-origin policy and hence can not be accessed by the requesting script through document.cookie or from response headers.