美文网首页
mock数据方法

mock数据方法

作者: 沉沉___ | 来源:发表于2019-02-14 14:36 被阅读0次

    方法1、线上githubpagesmock数据

    1. github创建一个项目:

    2. 分别创建文件:
      A、html文件 - home.html


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


      C、直接去githubpage去mock数据,如图:


    方法2、使用 easymock mock数据

    1. 进入https://easy-mock.com/
      创建url域名——创建接口——编辑json数据——更新、预览:即能看到返回的数据

    返回数据另一种方法:点击主页面生成的url,使用终端测试,也能返回相应数据
    $ curl url地址


    1. 使用项目文件,添加请求数据的文件,代码如下:
      <!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>

    2. 相应打开对应项目文件的url地址——检查——显示数据格式文件(定义好数据),说明所请求的数据已返回:


    相关文章

      网友评论

          本文标题:mock数据方法

          本文链接:https://www.haomeiwen.com/subject/nnqeeqtx.html