淺談Fetch 數(shù)據(jù)交互方式
獲取資源很簡單,發(fā)起一個請求出去,一個響應進來,然后該怎么操作就怎么操作。
fetch 的 api 使用的是 promise 規(guī)范,不會 promise(用于延遲(deferred) 計算和異步(asynchronous ) 計算。 一個 Promise 對象代表著一個還未完成,但預期將來會完成的操作。主要使用它處理回調(diào)黑洞。) 的請花幾分鐘學習一下。
使用 fetch 去獲取數(shù)據(jù)的方式:
fetch("服務器地址")
.then(function(response) {
//將獲取到的數(shù)據(jù)使用 json 轉(zhuǎn)換對象
return response.json();
})
.then(function(data) {
//獲取轉(zhuǎn)換后的格式
console.log(data);
})
//如果有異常會在 catch 捕獲
.catch(function(e) {
console.log("error");
});
有沒有發(fā)現(xiàn)使用 fetch 后代碼變優(yōu)美了,不關(guān)心數(shù)據(jù)怎么請求的,把更多的精力放在處理數(shù)據(jù)上。
不用寫回調(diào)函數(shù)了,也不用監(jiān)聽 xhr readystatechange 事件了,當數(shù)據(jù)請求回來后會傳遞給 then, 有異常就會直接觸發(fā) catch 事件。
fetch 默認發(fā)起的是 get 請求,如果需要 post 請求需要設置 Request
Request
Request 客戶端向服務器發(fā)出請求的一個對象,包括用戶提交的信息以及客戶端的一些信息
使用 Request 構(gòu)造一個 fetch 請求的對象的詳細信息
//實例化 request 對象
var myRequest = new Request(url, Option);
fetch(myRequest)
.then(function(response) {
console.log(response);
})
//如果有異常會在 catch 捕獲
.catch(function(e) {
console.log("error");
});
Request 詳細參數(shù)配置:
method
設置請求方式
method = GET / POST / PUT / DELETE / HEAD
headers
設置請求頭信息,使用 Headers 對象
let headers = new Headers({
'Content-Type': 'text/plain'
});
mode
請求的模式,主要用于跨域設置
mode = cors / no-cors / same-origin
cors : 跨域
no-cors : 不跨域
same-origin : 同源
credentials
需要向服務器發(fā)送 cookie 時設置
credentials = omit / same-origin
omit : 省略
same-origin : 發(fā)送同源 cookie
cache
cache = default / reload / no-cache
redirect
收到重定向消息時如何處理
redirect = follow / error / manual
follow : 跟隨重定向的地址 ,繼續(xù)請求
error : 不請求
比如:
var request = new Request("url", {
headers: new Headers({
"Content-Type": "text/plain"
}),
method : "POST",
mode: "cors",
redirect : "follow"
});
fetch(request)
.then((response) => {
console.log(response);
})
.catch((error)=>{
console.log(error);
});
fetch 數(shù)據(jù)處理
當 fetch 把請求結(jié)果拿到后,我們需要使用它提供的幾個方法來做處理
json
fetch 提供了一個 json 方法將數(shù)據(jù)轉(zhuǎn)換為 json 格式
fetch(url)
.then((response) => {
//返回 object 類型
return response.json();
})
.then((result) => {
console.log(result);
});
text
fetch 提供了一個 text 方法用于獲取數(shù)據(jù),返回的是 string 格式數(shù)據(jù)
fetch(url)
.then((response) => {
//返回 string 類型
return response.text();
})
.then((result) => {
console.log(result);
});
blob
如果我們獲取的是一個圖像,需要先設置頭信息,然后 fetch 會正常處理本次請求,最終使用 blob 方法獲取本次請求的結(jié)果, 可以把結(jié)果賦值給 img src 就能正常的顯示一張圖片
var request = new Request("xx.img", {
headers: new Headers({
"Content-Type": "image/jpeg"
}),
method : "get",
cache: 'default'
});
fetch(request)
.then((response) => {
return response.blob();
})
.then((stories)=>{
var objectURL = URL.createObjectURL(stories);
let img = document.createElement("img");
img.src = objectURL;
document.querySelector("body").appendChild(img);
});
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Echart結(jié)合圓形實現(xiàn)儀表盤的繪制詳解
EChart開源來自百度商業(yè)前端數(shù)據(jù)可視化團隊,基于html5?Canvas,是一個純Javascript圖表庫,提供直觀,生動,可交互,可個性化定制的數(shù)據(jù)可視化圖表。本文將利用EChart實現(xiàn)儀表盤的繪制,感興趣的可以學習一下2022-03-03
JavaScript編寫Chrome擴展實現(xiàn)與瀏覽器的交互及時間通知
得益于API,我們可以用JavaScript編寫Chrome擴展實現(xiàn)與瀏覽器的交互及時間通知。值得一提的是現(xiàn)在Chrome擁有后臺進程可以使通知在前臺瀏覽器關(guān)閉的情況下依然能夠生效.2016-05-05
js實現(xiàn)屏蔽默認快捷鍵調(diào)用自定義事件示例
本文要說的是如何屏蔽默認的快捷鍵后去執(zhí)行自定義的事件,下面以一個textarea中enter進行保存的例子為大家詳細介紹下,感興趣的朋友可以參考下哈2013-06-06

