Ajax Testing

You can test ajax requests in labs. The implementation is very basic and simple that gives you more flexibility as to what you want to do.

When you are developing on labs, you can't really get the data from your server as that would require cross origin requests that is disabled at browser level unless you use CORS or maybe JSONP.

So you if you want you could "mock" the data that you would like to get on response. Mock objects/data are basically fake ones that mimic the real objects that you would expect on response.

Here's some code:

// Let's say this is the object you want on Response
var content = {text: "via Ajax"};
// Simply JSON encode it
content = JSON.stringify(content);

// POST XHR with jQuery
$.post(
  // Request URL
  '/labs/echo',

  // Request Data. Note the 'resBody' property
  {resBody: content},

  function(data) {
    // `data` is the object you sent in the Request
    // Now you can do whatever you want to, with it

    // DOM Manipulation
    $('#some_id').html(data.text);
  },

  // Respons Data Type expected
  'json'
);

So we sent some JSON data to the server and the server responded back with the same data. jQuery converts that to a JS object (you could use JSON.parse()) and we use that object to do basic DOM Manipulation.

Hence, you are going to get back whatever data you POST to /labs/echo. We could also send some HTML data with JS code in script tags and inject that to the DOM and do some cool stuffs.

There are 2 parameters that you can send in the POST Request Data:

resBody
Data sent back as the Response Body.
resType
optional. Use this to set the Response's Content-Type header.

Now it's up to you whether you want to send JSON and then convert response data into JS object or you want to sent plain text, XML or HTML.