html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/Mock.js/1.0.0/mock-min.js"></script>
</head>
<body>
<script>MOCK = true</script>
<script src="./mock/index.js"></script>
<script>
$.ajax({
url: '/user/userinfo',
type: 'get',
dataType: 'json',
success: (data) => {
console.log(data)
}
})
$.ajax({
url: '/user/userinfo',
type: 'post',
dataType: 'json',
success: (data) => {
console.log(data)
}
})
</script>
</body>
</html>
index.js文件:地址保持一样才能拦截
if (MOCK == true) {
Mock.mock('/user/userinfo', 'get', {
id: '@id',
username: '@cname',
date: '@date',
description: '@paragraph'
})
Mock.mock('/user/userinfo', 'post', {
id: '@id'
})
}