立即开始
基于 Promise 的 HTTP 客户端,适用于浏览器和 node.js
什么是 Axios?
¥What is Axios?
Axios 是一个 基于 promise 的 HTTP 客户端,适用于 node.js 和浏览器。它是 同构的(= 它可以在同一代码库的浏览器和 Node.js 中运行)。在服务器端它使用原生的 node.js http 模块,在客户端(浏览器)它使用 XMLHttpRequests。
¥Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and node.js with the same codebase). On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests.
特性
¥Features
从浏览器发出 XMLHttpRequests
¥Make XMLHttpRequests from the browser
从 node.js 发出 http 请求
¥Make http requests from node.js
支持 Promise API
¥Supports the Promise API
拦截请求和响应
¥Intercept request and response
转换请求和响应数据
¥Transform request and response data
取消请求
¥Cancel requests
超时
¥Timeouts
支持嵌套条目的查询参数序列化
¥Query parameters serialization with support for nested entries
自动请求主体序列化为:
¥Automatic request body serialization to:
JSON (application/json)
多部分/表单数据 (multipart/form-data)
¥Multipart / FormData (multipart/form-data)
URL 编码形式 (application/x-www-form-urlencoded)
¥URL encoded form (application/x-www-form-urlencoded)
将 HTML 表单作为 JSON 发送
¥Posting HTML forms as JSON
响应中的自动 JSON 数据处理
¥Automatic JSON data handling in response
浏览器和 node.js 的进度捕获,并提供额外的信息(速度、剩余时间)
¥Progress capturing for browsers and node.js with extra info (speed rate, remaining time)
为 node.js 设置带宽限制
¥Setting bandwidth limits for node.js
兼容符合规范的 FormData 和 Blob(包括 node.js)
¥Compatible with spec-compliant FormData and Blob (including node.js)
客户端支持防御 XSRF
¥Client side support for protecting against XSRF
安装
¥Installing
使用 npm:
¥Using npm:
$ npm install axios
使用 bower:
¥Using bower:
$ bower install axios
使用 yarn:
¥Using yarn:
$ yarn add axios
使用 pnpm:
¥Using pnpm:
$ pnpm add axios
使用 jsDelivr CDN:
¥Using jsDelivr CDN:
使用 unpkg CDN:
¥Using unpkg CDN:
预建 CommonJS 模块,可通过 require 直接导入(如果你的模块打包器无法自动解析它们)。
¥Prebuilt CommonJS modules for direct importing with require (if your module bundler failed to resolve them automatically)
const axios = require('axios/dist/browser/axios.cjs'); // browser
const axios = require('axios/dist/node/axios.cjs'); // node
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert('Copied to clipboard!');
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
下一章 »
示例