一. 請求純文本
- 在本地服務(wù)器環(huán)境下
- 創(chuàng)建XMLHttpRequest對象
- xhr的open方法,用于配置請求相關(guān)信息,open方法有三個參數(shù)。第一個參數(shù)type,數(shù)據(jù)傳輸方式get或post。第二個參數(shù)url/file,請求路徑。第三個參數(shù)async是否異步。
- 配置好后,要將路徑中(sample.txt)的東西請求下來。分兩種請求方式
(1)onload
(2)onreadystatechange - 利用
onload請求,上節(jié)中講到服務(wù)器返回數(shù)據(jù)時會傳輸HTML Response,所以this.responseText獲取的就是返回的數(shù)據(jù)。 - 全部配置好后,還不能獲取數(shù)據(jù)。運用send方法用于發(fā)送請求。
- 點擊按鈕發(fā)起請求,請求成功
-
onreadystatechange請求方式,可以請求,并且發(fā)現(xiàn)實際上請求了兩次。
xhr.onreadystatechange = function(){
console.log(this.responseText);
}





