方法1、线上githubpagesmock数据
github创建一个项目:
-
分别创建文件:
A、html文件 - home.html
B、Json文件 - cate.json(注意路径)
C、直接去githubpage去mock数据,如图:
方法2、使用 easymock mock数据
- 进入https://easy-mock.com/
创建url域名——创建接口——编辑json数据——更新、预览:即能看到返回的数据
返回数据另一种方法:点击主页面生成的url,使用终端测试,也能返回相应数据
$ curl url地址
使用项目文件,添加请求数据的文件,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest()
//所请求的数据url地址
xhr.open('GET','https://easy-mock.com/mock/5c64fb6bfbee5808a30312da/ccccc/getType',true)
xhr.send()
xhr.addEventListener('load',function(){
console.log(xhr.status)
if((xhr.status >= 200 && xhr.status <300) ||xhr.status ===304){
var data = xhr.responseText
console.log(data)
}else{
console.log('error')
}
})
xhr.onerror = function(){
console.log('error')
}
</script>
</body>
</html>-
相应打开对应项目文件的url地址——检查——显示数据格式文件(定义好数据),说明所请求的数据已返回: