方法1、線上githubpagesmock數(shù)據(jù)
github創(chuàng)建一個項目:
-
分別創(chuàng)建文件:
A、html文件 - home.html
B、Json文件 - cate.json(注意路徑)
C、直接去githubpage去mock數(shù)據(jù),如圖:
方法2、使用 easymock mock數(shù)據(jù)
- 進(jìn)入https://easy-mock.com/
創(chuàng)建url域名——創(chuàng)建接口——編輯json數(shù)據(jù)——更新、預(yù)覽:即能看到返回的數(shù)據(jù)
返回數(shù)據(jù)另一種方法:點擊主頁面生成的url,使用終端測試,也能返回相應(yīng)數(shù)據(jù)
$ curl url地址

使用項目文件,添加請求數(shù)據(jù)的文件,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest()
//所請求的數(shù)據(jù)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>-
相應(yīng)打開對應(yīng)項目文件的url地址——檢查——顯示數(shù)據(jù)格式文件(定義好數(shù)據(jù)),說明所請求的數(shù)據(jù)已返回:




