獲取資源很簡單,發(fā)起一個請求出去,一個響應(yīng)進來,然后該怎么操作就怎么操作。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),霍林郭勒企業(yè)網(wǎng)站建設(shè),霍林郭勒品牌網(wǎng)站建設(shè),網(wǎng)站定制,霍林郭勒網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,霍林郭勒網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
fetch 的 api 使用的是 promise 規(guī)范,不會 promise(用于延遲(deferred) 計算和異步(asynchronous ) 計算。 一個 Promise 對象代表著一個還未完成,但預(yù)期將來會完成的操作。主要使用它處理回調(diào)黑洞。) 的請花幾分鐘學(xué)習(xí)一下。
使用 fetch 去獲取數(shù)據(jù)的方式:
有沒有發(fā)現(xiàn)使用 fetch 后代碼變優(yōu)美了,不關(guān)心數(shù)據(jù)怎么請求的,把更多的精力放在處理數(shù)據(jù)上。
不用寫回調(diào)函數(shù)了,也不用監(jiān)聽 xhr readystatechange 事件了,當(dāng)數(shù)據(jù)請求回來后會傳遞給 then, 有異常就會直接觸發(fā) catch 事件。
fetch 默認(rèn)發(fā)起的是 get 請求,如果需要 post 請求需要設(shè)置 Request
Request
Request 客戶端向服務(wù)器發(fā)出請求的一個對象,包括用戶提交的信息以及客戶端的一些信息
使用 Request 構(gòu)造一個 fetch 請求的對象的詳細(xì)信息
//實例化 request 對象 var myRequest = new Request(url, Option); fetch(myRequest) .then(function(response) { console.log(response); }) //如果有異常會在 catch 捕獲 .catch(function(e) { console.log("error"); });
Request 詳細(xì)參數(shù)配置:
method
設(shè)置請求方式
method = GET / POST / PUT / DELETE / HEAD
headers
設(shè)置請求頭信息,使用 Headers 對象
let headers = new Headers({ 'Content-Type': 'text/plain' });
mode
請求的模式,主要用于跨域設(shè)置
mode = cors / no-cors / same-origin
cors : 跨域
no-cors : 不跨域
same-origin : 同源
credentials
需要向服務(wù)器發(fā)送 cookie 時設(shè)置
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ù)處理
當(dāng) 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
如果我們獲取的是一個圖像,需要先設(shè)置頭信息,然后 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享標(biāo)題:淺談Fetch數(shù)據(jù)交互方式
轉(zhuǎn)載來于:http://chinadenli.net/article22/gioscc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、搜索引擎優(yōu)化、手機網(wǎng)站建設(shè)、ChatGPT、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)