Axios 是一个基于 Promise 的 HTTP 客户端,可以用于浏览器和 node.js 中。

要使用 Axios,首先需要在项目中安装它:

npm install axios

然后,可以在你的代码中引入 Axios:

const axios = require('axios');

或者,如果你使用的是 ES6 的语法:

import axios from 'axios';

然后,就可以使用 Axios 的各种方法来发送 HTTP 请求了。

例如,你可以使用 axios.get() 方法来发送一个 GET 请求:

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

你也可以使用 axios.post() 方法来发送一个 POST 请求:

axios.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Axios 还支持其他类型的 HTTP 请求,比如 PUT、DELETE 等。有关更多信息,请参阅 Axios 的文档:https://github/axios/axios

更多推荐

chatGPT回答如何使用axios?