成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Axios vs. fetch():哪個最適合 HTTP 請求?

開發 前端
在本文中,我將按照基本語法、向后兼容性、響應超時、自動JSON數據轉換、HTTP攔截器、下載進度、同時請求這些方面來比較fetch()和Axios,看看它們如何執行任務。

因為Axios的易于使用,所以有些開發人員比起內置的API,更喜歡Axios。

但許多人高估了這個庫。

fetch() API不但完全能夠重現Axios的關鍵功能,而且還有隨時可用于所有現代瀏覽器中的獨特優勢。

在本文中,我將按照基本語法、向后兼容性、響應超時、自動JSON數據轉換、HTTP攔截器、下載進度、同時請求這些方面來比較fetch()和Axios,看看它們如何執行任務。

希望在本文結束時,大家對這兩個API有了更深入的了解。

基本語法

在我們深入研究Axios更高級地功能之前,先與fetch()進行基本語法的比較。

下面是Axios如何將帶有自定義請求頭的[POST]請求發送到指定URL的代碼:

// axios

const url = 'https://jsonplaceholder.typicode.com/posts'
const data = {
  a: 10,
  b: 20,
};
axios
  .post(url, data, {
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json;charset=UTF-8",
    },
  })
  .then(({data}) => {
    console.log(data);
});

與fetch()版本進行比較:

// fetch()

const url = "https://jsonplaceholder.typicode.com/todos";
const options = {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json;charset=UTF-8",
  },
  body: JSON.stringify({
    a: 10,
    b: 20,
  }),
};
fetch(url, options)
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  });

注意:

  • 為發送數據,fetch()使用body屬性將數據發送到服務端,而Axios使用data屬性
  • fetch()中的數據使用JSON.stringify方法轉換為字符串
  • Axios自動轉換從服務器返回的數據,但使用fetch()時,你必須調用response.json方法將數據解析為JavaScript對象。
  • 使用Axios,服務器提供的數據響應可以在數據對象中訪問,而對于fetch()方法,最終數據可以命名為任何變量

向后兼容性

Axios的主要賣點之一是其廣泛的瀏覽器支持。

即使是像IE11這樣的舊瀏覽器也可以毫無問題地運行Axios。這是因為它背后使用了XMLHttpRequest。

而fetch()僅支持Chrome 42+,Firefox 39+,Edge 14+和Safari 10.3+。

如果你使用Axios的唯一原因是向后兼容性,那么實際上并不需要HTTP庫。而且,你可以將fetch()與polyfill一起使用,在不支持fetch()的web瀏覽器上實現類似的功能。

要使用fetch() polyfill,可以通過npm命令進行安裝,如下所示:

npm install whatwg-fetch --save

然后,提出如下請求:

import 'whatwg-fetch'
window.fetch(...)

謹記,在有些舊瀏覽器中,可能還需要promise polyfill。

響應超時

在Axios中設置超時的簡單性,是一些開發人員比fetch()更喜歡Axios的原因之一。

在Axios中,你可以使用配置對象的timeout屬性來設置請求中止之前的毫秒數。

例如:

axios({
  method: 'post',
  url: '/login',
  timeout: 4000,    // 4 seconds timeout
  data: {
    firstName: 'David',
    lastName: 'Pollock'
  }
})
.then(response => {/* handle the response */})
.catch(error => console.error('timeout exceeded'))

Fetch()通過AbortController接口提供類似的功能。

不過,它的代碼不如Axios版本簡單:

const controller = new AbortController();
const options = {
  method: 'POST',
  signal: controller.signal,
  body: JSON.stringify({
    firstName: 'David',
    lastName: 'Pollock'
  })
};  
const promise = fetch('/login', options);
const timeoutId = setTimeout(() => controller.abort(), 4000);

promise
  .then(response => {/* handle the response */})
  .catch(error => console.error('timeout exceeded'));

代碼使用AbortController.abort()構造函數創建AbortController對象,它允許我們稍后中止請求。

Signal是AbortController的只讀屬性,提供了一種與請求通信或中止請求的方法。

如果服務器在4秒內沒有響應,則調用controller.abort(),終止操作。

自動JSON數據轉換

如前所述,Axios在發送請求時會自動字符串化數據(當然你也可以覆蓋默認行為并定義不同的轉換機制)。

但是,當使用fetch()時,你必須手動執行此操作。

比較:

// axios
axios.get('https://api.github.com/orgs/axios')
  .then(response => {
    console.log(response.data);
  }, error => {
    console.log(error);
  });
// fetch()
fetch('https://api.github.com/orgs/axios')
  .then(response => response.json())    // one extra step
  .then(data => {
    console.log(data) 
  })
  .catch(error => console.error(error));

自動轉換數據是一個不錯的功能,但同樣,這不是你不能用fetch()做的事情。

HTTP攔截器

Axios的主要功能之一是它能夠攔截HTTP請求。

當你需要檢查或更改從應用程序到服務器的HTTP請求時,使用HTTP攔截器非常方便,從服務器到應用程序亦是如此(例如,日志記錄、身份驗證或重試失敗的HTTP請求)。

使用攔截器就不必為每個HTTP請求編寫單獨的代碼。

在你想要為處理請求和響應設置全局策略時,HTTP攔截器非常有用。

以下是在Axios中聲明請求攔截器的方法:

axios.interceptors.request.use(config => {
  // log a message before any HTTP request is sent
  console.log('Request was sent');

  return config;
});

// sent a GET request
axios.get('https://api.github.com/users/sideshowbarker')
  .then(response => {
    console.log(response.data);
  });

上面的代碼中,axios.interceptors.request.use()方法用于定義發送HTTP請求之前要運行的代碼。而axios.interceptors.response.use()用于攔截來自服務器的響應。

假設存在網絡錯誤,那么通過響應偵聽器,可以重試相同的請求。

默認情況下,fetch()不提供攔截請求的方法,但它的解決方法也并不復雜。

那就是覆蓋全局fetch()方法并定義自己的攔截器,如下所示:

fetch = (originalFetch => {
  return (...arguments) => {
    const result = originalFetch.apply(this, arguments);
      return result.then(console.log('Request was sent'));
  };
})(fetch);

fetch('https://api.github.com/orgs/axios')
  .then(response => response.json())
  .then(data => {
    console.log(data) 
  });

下載進度

進度條在加載時非常有用,尤其是對于互聯網速度較慢的用戶。

以前,JavaScript程序員使用XMLHttpRequest.onprogress回調處理程序來實現進度指示器。

Fetch API沒有onprogress處理程序。事實上,它通過響應對象的body屬性來提供ReadableStream的實例。

以下示例表明如何使用ReadableStream在圖像下載期間為用戶提供即時反饋:

index.html
<!-- Wherever you html is -->
  <div id="progress" src="">progress</div>
  <img id="img">

script.js
'use strict'
const element = document.getElementById('progress');
fetch('https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg')
  .then(response => {
    if (!response.ok) {
      throw Error(response.status+' '+response.statusText)
    }
    // ensure ReadableStream is supported
    if (!response.body) {
      throw Error('ReadableStream not yet supported in this browser.')
    }
    // store the size of the entity-body, in bytes
    const contentLength = response.headers.get('content-length');
    // ensure contentLength is available
    if (!contentLength) {
      throw Error('Content-Length response header unavailable');
    }
    // parse the integer into a base-10 number
    const total = parseInt(contentLength, 10);
    let loaded = 0;
    return new Response(
      // create and return a readable stream
      new ReadableStream({
        start(controller) {
          const reader = response.body.getReader();
          read();
          function read() {
            reader.read().then(({done, value}) => {
              if (done) {
                controller.close();
                return; 
              }
              loaded += value.byteLength;
              progress({loaded, total})
              controller.enqueue(value);
              read();
            }).catch(error => {
              console.error(error);
              controller.error(error)                  
            })
          }
        }
      })
    );
  })
  .then(response => 
    // construct a blob from the data
    response.blob()
  )
  .then(data => {
    // insert the downloaded image into the page
    document.getElementById('img').src = URL.createObjectURL(data);
  })
  .catch(error => {
    console.error(error);
  })
function progress({loaded, total}) {
  element.innerHTML = Math.round(loaded/total*100)+'%';
}

在Axios中實現進度指示器更簡單,尤其是在使用Axios進度條模塊時。

首先,包含以下樣式和腳本:

// the head of your HTML
    <link rel="stylesheet" type="text/css"
         />


// the body of your HTML
     <img id="img" />
    <button onclick="downloadFile()">Get Resource</button>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.rawgit.com/rikmms/progress-bar-4-axios/0a3acf92/dist/index.js"></script>

// add the following to customize the style

<style>
    #nprogress .bar {
        background: red !important;
    }
    #nprogress .peg {
        box-shadow: 0 0 10px red, 0 0 5px red !important;
    }
    #nprogress .spinner-icon {
        border-top-color: red !important;
        border-left-color: red !important;
    }
</style>

然后像這樣實現進度條:

<script type="text/javascript">
        loadProgressBar();

        function downloadFile() {
          getRequest(
            "https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg"
          );
        }

        function getRequest(url) {
          axios
            .get(url, { responseType: "blob" })
            .then(function (response) {
              const reader = new window.FileReader();
              reader.readAsDataURL(response.data);
              reader.onload = () => {
                document.getElementById("img").setAttribute("src", reader.result);
              };
            })
            .catch(function (error) {
              console.log(error);
            });
        }
      </script>

代碼使用FileReaderAPI異步讀取下載的圖像。

readAsDataURL方法以Base64編碼字符串的形式返回圖像的數據,然后將其插入到img標記的src屬性中以顯示圖像。

并發請求

為了同時發出多個請求,Axios提供axios.all()方法。

只需將請求數組傳遞給此方法,然后使用axios.spread()將響應數組的屬性分配給單獨的變量:

axios.all([
  axios.get('https://api.github.com/users/iliakan'), 
  axios.get('https://api.github.com/users/taylorotwell')
])
.then(axios.spread((obj1, obj2) => {
  // Both requests are now complete
  console.log(obj1.data.login + ' has ' + obj1.data.public_repos + ' public repos on GitHub');
  console.log(obj2.data.login + ' has ' + obj2.data.public_repos + ' public repos on GitHub');
}));

也可以使用內置的Promise.all()方法獲得相同的結果。

將所有fetch請求作為數組傳遞給Promise.all()。接著使用async函數處理響應,如下所示:

Promise.all([
  fetch('https://api.github.com/users/iliakan'),
  fetch('https://api.github.com/users/taylorotwell')
])
.then(async([res1, res2]) => {
  const a = await res1.json();
  const b = await res2.json();
  console.log(a.login + ' has ' + a.public_repos + ' public repos on GitHub');
  console.log(b.login + ' has ' + b.public_repos + ' public repos on GitHub');
})
.catch(error => {
  console.log(error);
});

結論

Axios在緊湊的軟件包中提供了一個易于使用的API,可滿足大多數HTTP通信需求。

而web瀏覽器提供的fetch()方法則能完全重現Axios庫的主要功能。

所以,是否加載客戶端HTTP API取決于你是否習慣使用內置API。

編程快樂!

責任編輯:武曉燕 來源: 前端新世界
相關推薦

2017-01-15 11:14:47

超融合數據中心IT基礎設施

2017-11-29 14:48:01

Node.JSRails語言

2017-03-09 13:30:13

Linux游戲AMD

2022-12-26 14:51:48

人工智能

2023-07-10 09:18:39

Redis訂閱模型

2019-09-01 19:19:04

TensorFlowPyTorch深度學習

2020-03-17 15:55:12

Redis數據庫命令

2023-01-13 10:46:42

2012-05-16 11:53:39

虛擬化

2022-07-11 10:17:19

Swift編程語言項目

2009-01-19 16:54:50

數據挖掘CRM孤立點

2016-01-26 09:58:28

云存儲云服務云安全

2023-10-08 13:42:00

Python Web框架

2015-12-08 09:31:02

Linux系統操作系統

2017-06-27 15:08:05

大數據Apache SparKafka Strea

2012-03-20 09:32:24

Linux服務器

2018-07-16 08:50:31

固態硬盤內存

2018-09-07 06:30:50

物聯網平臺物聯網IOT

2021-02-14 10:09:04

數據目錄數據元數據

2019-03-10 22:21:47

框架AI開發
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩一区二区免费视频 | 日本午夜精品一区二区三区 | 男人av在线| 久久久久久高潮国产精品视 | 国产女人与拘做受视频 | 中文在线一区二区 | 欧美成人二区 | 国产成人99久久亚洲综合精品 | 天天久久| 乱码av午夜噜噜噜噜动漫 | 亚洲免费人成在线视频观看 | 第一区在线观看免费国语入口 | 亚洲精品日韩一区二区电影 | 第四色狠狠| av在线视| 久久国产欧美日韩精品 | 999久久久 | 欧美一区2区三区4区公司 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 久久亚洲欧美日韩精品专区 | 国产高清一区二区三区 | 久久久久久艹 | 欧美精品一区二区免费 | xx视频在线 | 鸡毛片 | 精品中文字幕一区二区 | 午夜成人在线视频 | 日韩精品一区在线观看 | 一区二区久久 | a级黄色片在线观看 | 国产一区在线免费观看 | 亚洲国产精品一区 | 国产在线一级片 | 欧美视频免费在线观看 | 狠狠爱免费视频 | 久久久.com| 日韩国产欧美一区 | 91毛片网 | 午夜黄色影院 | 欧美精品一区二区三区四区五区 | 特一级黄色毛片 |