前后端分离开发——模拟数据mock.js

mock.js 生成模拟数据,拦截ajax请求

 1     <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
 2     <script type="text/javascript" src="http://mockjs.com/dist/mock.js"></script>
 3     <script>
 4         Mock.mock('/api/test', {
 5             "array1|1-10" : [{
 6                 'name|+1' : ['Hello', 'Mock.js', '!']
 7             }],
 8         });
 9         $.ajax({
10             'url'    :    '/api/test',
11             'type'    :    'GET',
12         }).done(function(result){
13             console.log(result);
14         });
15     </script>

结果

{
    "array1": [
        {
            "name": "Hello"
        },
        {
            "name": "Mock.js"
        },
        {
            "name": "!"
        }
    ]
}

数组array1的大小在[1-10]范围内随机变动,数组元素  { 'name|+1' : ['Hello', 'Mock.js', '!'] } ,表示 name的值在数组 ['Hello', 'Mock.js', '!'] 中随机选择

原文地址:https://www.cnblogs.com/lhat/p/6148275.html