mockjs模拟数据(ajax版)

使用mockjs模拟ajax可请求的url返回数据

/**
 * mark: 模拟数据的方法
 * author: LiuWei
 * time: 2017/8/17
 */
function mockDataFunc() {
    var mockData = Mock.mock('http://mockdata', {
        "maxPage": "8",
        "showData|10": [{
            "id|+1": 1, //递增的id
            "url": "@url", //随机生成的url
            "img": "@image('140x186', '@color', '#FFF', '@last')", //生成图片
            "title": "@ctitle(6,18)", //中文标题,若需英文,去掉前缀c
            "timeList|6-16": [{
                "id|+1": 1, //递增的id
                "time": "@datetime('yyyy.MM.dd HH:mm')",
                "url": "@url",
                "isPast|1": true
            }],
            "venue": "@city(true)",
            "minPrice|10-200.2": 200, //小数点前数字范围在10-500之内,小数点后位数为2
            "maxPrice|200-800.1-2": 220, //小数点前数字范围在10-800之内,小数点后位数为1-2位
            "tip": "@csentence(8,20)", //中文句子,若需英文,去掉前缀c
        }]
    });
    return mockData;
}


//发送ajax请求
$.ajax({
    type: 'get',
    url: 'http://mockdata',//mock模拟的url地址
    dataType: 'json',
    //data: dataJson,
    success: function (res) {
        //res即为mockjs模拟的数据

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log(XMLHttpRequest, textStatus, errorThrown);
    }

})
原文地址:https://www.cnblogs.com/lw5116/p/7791946.html