image.png
概述:
Mock.js是一个模拟数据生成器,可以让前端独立于后端进行开发。如果你正在开发一个前端页面,可是后台还没有完成供你调用的API,并且数据格式已经确定,那么你就可以使用Mock.js模拟相关的接口,生成假数据来查看页面的效果。Mock.js的功能,生成随机数据,拦截Ajax请求。
如何mock数据?
1. 安装Node.js,通过http-server开启本地服务器。
获取端口http://127.0.0.1:8080
然后通过在文件夹创建html和json
来测试数据
实例:
//html
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET','/hello.json',true);
xhr.send();
xhr.onload = function(){
if(xhr.status===200){
var data = xhr.responseText;
console.log(JSON.parse(data));