立即开始

基于 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

安装

¥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:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

使用 unpkg CDN:

¥Using unpkg CDN:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<button onclick="copyToClipboard('<script src="https://unpkg.com/axios/dist/axios.min.js"></script>')">Copy</button>

预建 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

<button onclick="copyToClipboard('const axios = require('axios/dist/browser/axios.cjs');
const axios = require('axios/dist/node/axios.cjs'); // node')">Copy</button>

<script> function copyToClipboard(text) { navigator.clipboard.writeText(text).then(() => { alert('Copied to clipboard!'); }).catch(err => { console.error('Failed to copy: ', err); }); } </script>