使用JavaScript怎么實(shí)現(xiàn)一個(gè)fetch接口-創(chuàng)新互聯(lián)

本篇文章為大家展示了使用JavaScript怎么實(shí)現(xiàn)一個(gè)fetch接口,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了綏德免費(fèi)建站歡迎大家使用!

fetch獲取后端數(shù)據(jù)的例子:

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html') // 返回一個(gè)Promise對(duì)象
 .then((res)=>{
  return res.text() // res.text()是一個(gè)Promise對(duì)象
 })
 .then((res)=>{
  console.log(res) // res是最終的結(jié)果
 })

GET請(qǐng)求

GET請(qǐng)求初步

完成了helloworld,這個(gè)時(shí)候就要來認(rèn)識(shí)一下GET請(qǐng)求如何處理了。

上面的helloworld中這是使用了第一個(gè)參數(shù),其實(shí)fetch還可以提供第二個(gè)參數(shù),就是用來傳遞一些初始化的信息。

這里如果要特別指明是GET請(qǐng)求,就要寫成下面的形式:

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html', {
  method: 'GET'
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

GET請(qǐng)求的參數(shù)傳遞

GET請(qǐng)求中如果需要傳遞參數(shù)怎么辦?這個(gè)時(shí)候,只能把參數(shù)寫在URL上來進(jìn)行傳遞了。

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html?a=1&b=2', { // 在URL中寫上傳遞的參數(shù)
  method: 'GET'
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

POST請(qǐng)求

與GET請(qǐng)求類似,POST請(qǐng)求的指定也是在fetch的第二個(gè)參數(shù)中:

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html', {
  method: 'POST' // 指定是POST請(qǐng)求
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

POST請(qǐng)求參數(shù)的傳遞

眾所周知,POST請(qǐng)求的參數(shù),一定不能放在URL中,這樣做的目的是防止信息泄露。

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html', {
  method: 'POST',
  body: new URLSearchParams([["foo", 1],["bar", 2]]).toString() // 這里是請(qǐng)求對(duì)象
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

設(shè)置請(qǐng)求的頭信息

在POST提交的過程中,一般是表單提交,可是,經(jīng)過查詢,發(fā)現(xiàn)默認(rèn)的提交方式是:Content-Type:text/plain;charset=UTF-8,這個(gè)顯然是不合理的。下面咱們學(xué)習(xí)一下,指定頭信息:

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html', {
  method: 'POST',
  headers: new Headers({
   'Content-Type': 'application/x-www-form-urlencoded' // 指定提交方式為表單提交
  }),
  body: new URLSearchParams([["foo", 1],["bar", 2]]).toString()
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

這個(gè)時(shí)候,在谷歌瀏覽器的Network中查詢,會(huì)發(fā)現(xiàn),請(qǐng)求方式已經(jīng)變成了content-type:application/x-www-form-urlencoded。

通過接口得到JSON數(shù)據(jù)

上面所有的例子中都是返回一個(gè)文本,那么除了文本,有沒有其他的數(shù)據(jù)類型呢?肯定是有的,具體查詢地址:Body的類型

由于最常用的是JSON數(shù)據(jù),那么下面就簡(jiǎn)單演示一下獲取JSON數(shù)據(jù)的方式:

fetch('https://www.baidu.com/rec?platform=wise&ms=1&rset=rcmd&word=123&qid=11327900426705455986&rq=123&from=844b&baiduid=A1D0B88941B30028C375C79CE5AC2E5E%3AFG%3D1&tn=&clientWidth=375&t=1506826017369&r=8255', { // 在URL中寫上傳遞的參數(shù)
  method: 'GET',
  headers: new Headers({
   'Accept': 'application/json' // 通過頭指定,獲取的數(shù)據(jù)類型是JSON
  })
 })
 .then((res)=>{
  return res.json() // 返回一個(gè)Promise,可以解析成JSON
 })
 .then((res)=>{
  console.log(res) // 獲取JSON數(shù)據(jù)
 })

強(qiáng)制帶Cookie

默認(rèn)情況下, fetch 不會(huì)從服務(wù)端發(fā)送或接收任何 cookies, 如果站點(diǎn)依賴于維護(hù)一個(gè)用戶會(huì)話,則導(dǎo)致未經(jīng)認(rèn)證的請(qǐng)求(要發(fā)送 cookies,必須發(fā)送憑據(jù)頭).

// 通過fetch獲取百度的錯(cuò)誤提示頁面
fetch('https://www.baidu.com/search/error.html', {
  method: 'GET',
  credentials: 'include' // 強(qiáng)制加入憑據(jù)頭
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

上述內(nèi)容就是使用JavaScript怎么實(shí)現(xiàn)一個(gè)fetch接口,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:使用JavaScript怎么實(shí)現(xiàn)一個(gè)fetch接口-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.muchs.cn/article26/depejg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、標(biāo)簽優(yōu)化ChatGPT全網(wǎng)營(yíng)銷推廣、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)