简单使用
浏览器环境
使用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);
})
Comments NOTHING