01-Axios简单使用

nobility 发布于 2022-02-18 787 次阅读


简单使用

浏览器环境

使用script标签引入mockjs即可

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>hello world</title>
  <script src="http://mockjs.com/dist/mock.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
  <script>
    axios.get("/api")
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      })

    Mock.mock("/api", { //mock拦截Ajax
      "list|10": [{
        id: "@id",
        name: "@cname"
      }]
    })
  </script>
</body>

</html>

NodeJs环境

使用npm install axios安装到项目中,axios可作为爬虫工具,但是要注意是的在NodeJs环境中是无法使用Mock拦截Ajax请求的

const axios = require("axios");
axios.get("http://www.baidu.com/s?word=axios")
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  })
此作者没有提供个人介绍
最后更新于 2022-02-18