Generating mock data with MockJSON
Once you have mocked the HTTP call, you need to send some data in the response. You have different possibilities:
You can hand-write the data in the
responseText
attribute of the$.mockjax
call:$.mockjax({ url: '/products', type: 'GET', dataType: 'json', responseTime: 750, responseText: ['Here I can fake the response'] });
You can use a function to generate the mock data:
$.mockjax({ url: '/products', type: 'GET', dataType: 'json', responseTime: 750, response: function(settings) { var fake = 'We fake the url:'+settings.url; this.responseText = fake; } });
You can use a library that generates complex and random data in the response.
This third option can be performed with a library called
mockJSON
. You can download it from the GitHub repository at https://github.com/mennovanslooten/mockJSON.This library allows you to generate data templates to create random data. This helps you to keep your fake data more realistic. You can see on...