Skip to content

网络请求 ​

安装axios ​

在Webpack中安装axios ​

使用命令:

bash
yarn add axios

或者:

bash
npm install axios

在单独HTML文件中安装 ​

添加这一行

注意,下面的版本可能是过时的版本,详细点击这里查看

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

或者

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

使用axios ​

在Webpack中首先需要添加这一行:

js
const axios = require("axios");

在单独HTML页面中直接使用即可

Get请求 ​

js
axios.get('/link/to/get', {
  // 参数
  params: {
    ID: 12345
  },
  // 头信息
  headers: {
    token: 12345
  }
})
.then((response) => {
  // 处理成功情况
  console.log(response);
})
.catch((error) => {
  // 处理错误情况
  console.log(error);
})
.finally(() => {
  // 总是会执行
});

Post请求 ​

js
// formData为post内容
axios.post('/link/to/post', formData, {
  // 参数
  params: {
    ID: 12345
  },
  // 头信息
  headers: {
    token: 12345
  },
})
.then((response) => {
  // 处理成功情况
  console.log(response);
})
.catch((error) => {
  // 处理错误情况
  console.log(error);
})
.finally(() => {
  // 总是会执行
});

Released under the MIT License.