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 ...

Get KnockoutJS Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.