什么是Ajax?
ajax是一種技術方案,但并不是一種新技術。它依賴的是現有的CSS/HTML/Javascript,而其中最核心的依賴是瀏覽器提供的XMLHttpRequest對象,是這個對象使得瀏覽器可以發出HTTP請求與接收HTTP響應。 實現在頁面不刷新的情況下和服務端進行數據交互
如何實現Ajax?
- XMLHttpRequest對象
- fetch
XMLHttpRequest的用法、屬性
1.創建XHR對象
var xhr = new XMLHttpRequest();
2.xhr.open()
接受三個參數
- 1.要發送的請求的類型("get"、"post"),
- 請求的URL
- 是否異步發送請求的布爾值
3.xhr.send()
接受一個參數
- 1.請求主體發送的數據,不需要則傳入(null)
4.xhr.responseText:作為響應主體被返回的文本
5.xhr.responseXML:如果響應的內容類型是"text/html"或"application/xml",這個屬性中將保存包含著響應數據的XML DOM文檔
6.xhr.status:響應的HTTP狀態
7.xhr.statusText:HTTP狀態的說明
8.xhr.readyState:請求/響應過程的當前活動階段
- 0:未初始化。尚未調用open()方法。
- 1:啟動。已經調用open()方法,但尚未調用send()方法。
- 2:發送。已經調用send()方法,但尚未接收到響應。
- 3:接收。已經接收到部分響應數據。
- 4:完成。已經接收到全部數據,而且已經可以在客戶端使用。
說明:可以把xhr.readyState屬性和TCP/IP的三次握手類比一下,1,2表示第一次,3表示第二次,4表示第三次。只要readyState屬性變一次,就會觸發一次readyState事件。還要強調一點的是必須在調用open()之前指定onreadystatechage事件處理程序才能確保跨瀏覽器兼容性。
9.xhr.abort():取消異步請求,在接收響應之前使用
10.xhr.setRequestHeader():設置自定義的請求頭部信息。
在調用open()方法之后,send()方法之前使用。
11.xhr.timeout:表示請求在等待響應多少毫秒之后就終止。
12.xhr.overrideMimeType():用于重寫響應的MIME類型。
比如服務器返回的MIME類型時text/plain,但數據實際包含的是XML,根據MIME類型,即使數據是XML,responseXML屬性仍然是null,通過調用該方法,可以保證把響應當作XML而非純文本使用。調用方法必須在send()之前使用
var xhr = new XMLHttpReququest()
xhr.open("get","text.php",true)
xhr.overrideMimeType("text/html")
xhr.send(null)
具體實現Ajax
1.同步實現Ajax
hello.html
//同步執行Ajax
var xhr=new XMLHttpRequest();
xhr.open('GET','/hello.json',false);
xhr.send();
var data=xhr.responseText;
console.log(data);
hello.json
{
"name":"Ajax"
}
首先我們打開命令行本地服務器,不然會報錯。
報錯
接下來執行
大家可以看到紅色框的英文,大概意思就是主線程上的同步XMLHttpRequest由于對最終用戶體驗的不利影響而被淘汰。如需更多幫助,請檢查。所以我們需要使用異步執行Ajax
2. 請求方式為GET異步實現Ajax
hello.html
// 異步執行Ajax
var xhr=new XMLHttpRequest();
xhr.open('GET','/hello.json',true);
xhr.send();
xhr.addEventListener('load',function(){
var data=xhr.responseText;
console.log(data);
});
hello.json
{
"name":"Ajax"
}
image.png
大家注意一下,這里用Ajax向本地服務器請求的是/hello.json ,返回的數據類型是什么?是json對象還是什么?我們可以驗證一下。在源代碼添加一句 typeof data
image.png
結果為string
假如我們在open()當中輸入錯誤的會發現什么狀況呢?
404 not found
可以看到404 not found,404是http狀態碼,最開始執行成功狀態碼為200,于是我們可以繼續改進代碼,通過狀態碼判斷是否執行
hello.html
var xhr=new XMLHttpRequest();
xhr.open('GET','/hello.json',true);
xhr.send();
xhr.addEventListener('load',function(){
console.log(xhr.status);
if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
var data=xhr.responseText;
console.log(data);
}
else{
console.log('error');
}
});
//當網絡出現故障的時候
xhr.error=function(){
console.log("error");
}
當文件路徑沒有出錯的時候
假如我們要監聽當前執行的狀態,該怎么做?
hello.html
//監聽狀態
var xhr=new XMLHttpRequest();
console.log('readystatus:',xhr.readyState);
//使用readystatechange事件監聽狀態
xhr.addEventListener('readystatechange',function(){
console.log('readystatus:',xhr.readyState);
})
xhr.addEventListener('load',function(){
console.log(xhr.status);
if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
var data=xhr.responseText;
console.log(data);
}
else{
console.log('error');
}
});
xhr.open('GET','/hello.json',true);
xhr.send();
如果路徑出錯則是下圖這種狀況
image.png
由于是錯誤的路徑,所以沒有readyState:3,因為3表示接受響應數據,這步是不可能有的。
3.請求方式為POST異步實現Ajax
var xhr = new XMLHttpRequest()
xhr.timeout = 3000 //可選,設置xhr請求的超時時間
xhr.open('POST', '/login', true)
xhr.onload = function(e) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
console.log(this.responseText)
}
}
//可選
xhr.ontimeout = function(e) {
console.log('請求超時')
}
//可選
xhr.onerror = function(e) {
console.log('連接失敗')
}
//可選
xhr.upload.onprogress = function(e) {
//如果是上傳文件,可以獲取上傳進度
}
xhr.send('username=jirengu&password=123456')
POST方式將數據放到send()中
4.封裝Ajax
function ajax(opts){
var url = opts.url
var type = opts.type || 'GET'
var dataType = opts.dataType || 'json'
var onsuccess = opts.onsuccess || function(){}
var onerror = opts.onerror || function(){}
var data = opts.data || {}
var dataStr = []
for(var key in data){
dataStr.push(key + '=' + data[key])
}
dataStr = dataStr.join('&')
if(type === 'GET'){
url += '?' + dataStr
}
var xhr = new XMLHttpRequest()
xhr.open(type, url, true)
xhr.onload = function(){
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
//成功了
if(dataType === 'json'){
onsuccess( JSON.parse(xhr.responseText))
}else{
onsuccess( xhr.responseText)
}
} else {
onerror()
}
}
xhr.onerror = onerror
if(type === 'POST'){
xhr.send(dataStr)
}else{
xhr.send()
}
}
ajax({
url: 'http://api.jirengu.com/weather.php',
type:'POST',
dataType:'TEXT',
data: {
city: '北京'
},
onsuccess: function(ret){
console.log(ret)
render(ret) //瀏覽器渲染
},
onerror: function(){
console.log('服務器異常')
showError() //錯誤瀏覽器渲染
}
})
function render(json){
//拼裝成dom輸入頁面中
}
function showError(){
//錯誤提示
}