Mock

官方地址:http://mockjs.com/

生成随机数,拦截请求

基础用法

  var data = Mock.mock({
    "array|1-10": [
      {
        "id|+1": 0,
        "name":"@cname"
      }
    ],
    "code|1": 0
  })
  console.log(JSON.stringify(data, null, 2))

Random

  var Random = Mock.Random
  var fruits = ["apple", "banana", "pear", "oranger"]
  var data = {
    email: Random.email(),
    name: Random.cname(),
    image: Random.image("200x100", "#00405d", "#fff", "Mock"),
    url: Random.url(),
    city: Random.city(),
    fruits: Random.pick(fruits),
  }
  console.log(JSON.stringify(data, null, 2))

请求拦截

 $("#app").click(function() {
   $.ajax({
     url: 'http://api.yourdomain.com/getlist/',
     type: 'get',
     dataType: 'json'
   }).done(function(data, status, xhr) {
     console.log(data, null, 2)
   })
 })
 var obj = {
    aa: '11',
    bb: '22',
    cc: '33',
    dd: '44'
  }
  Mock.mock('http://api.yourdomain.com/getlist/', {
    "user|1-3": [
      {
        "id|+1": 1,
        name: "@cname",
        "age|18-28": 0,
        birthday: '@date(yyyy-MM-dd)',
        city: {
          "number|1": "03443",
          "city|1": "北京"
        },
        "fromObj|2": obj
      }
    ]
  })

 

posted on 2021-04-30 15:43  苏荷酒吧  阅读(58)  评论(0编辑  收藏  举报