Axios 是目前最主流的前端 http 通讯库。
Axios 基于 promise,并且同时支持 browser 和 node 环境。
在 browser 环境下,Axios 利用的是 XMLHttpRequest。
在 node 环境,Axios 利用的是其自带的 http 模块。
如果要用一句话,来介绍 Axios 的话:
Promise based HTTP client for the browser and node.js
1-1.特性
Make XMLHttpRequests from the browserMake http requests from node.jsSupports the Promise APIIntercept request and responseTransform request and response dataCancel requestsTimeoutsQuery parameters serialization with support for nested entriesAutomatic request body serialization to:JSON (application/json)URL encoded form (application/x-www-form-urlencoded)Multipart / FormData (multipart/form-data)
Posting HTML forms as JSONAutomatic JSON data handling in responseProgress capturing for browsers and node.js with extra info (speed rate, remaining time)Setting bandwidth limits for node.jsCompatible with spec-compliant FormData and Blob (including node.js)Client side support for protecting against XSRF
1-2.安装
直接利用 yarn 安装依赖:
shell
yarn add axios或者,在浏览器端,利用 script 标签引入 cdn:
html
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>