mock數(shù)據(jù)方法

方法1、線上githubpagesmock數(shù)據(jù)

  1. github創(chuàng)建一個項目:

  2. 分別創(chuàng)建文件:
    A、html文件 - home.html


    B、Json文件 - cate.json(注意路徑)


    C、直接去githubpage去mock數(shù)據(jù),如圖:


方法2、使用 easymock mock數(shù)據(jù)

  1. 進(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地址


  1. 使用項目文件,添加請求數(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>

  2. 相應(yīng)打開對應(yīng)項目文件的url地址——檢查——顯示數(shù)據(jù)格式文件(定義好數(shù)據(jù)),說明所請求的數(shù)據(jù)已返回:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容