Class cf2:json.ajax

Purpose

Allows authors to integrate JSON and AJAX by extending the V$.ajax.Request and V$.ajax.Response classes.

Exported Features

Class cf2:json.ajax

Imported Features

cf2:json.Parser, cf2:ajax.Request - enhances

JavaScript

This component adds the following method to the V$.ajax.Request class.

bodyAsJSON(object)

Creates a JSON representation of the supplied object. This method is only valid for a request that uses either the post or put method, attempting to use it on any other method will result in an error. This method automatically sets the content type of the request to application/json.

Parameter Description Type
object A Map or an array that will be converted into a JSON string to send to a server. any

The following method extends the V$.ajax.Response class.

asJSON()

Treats the body of the response as a JSON string and parses it into the equivalent JavaScript object. If the response came from the same origin server, then the trusted JSON parser is used. If the response came from a different server, then the untrusted parser is used.

Example

onAjaxSuccess: function(boxId, response) {
  this.loadingQueue--;
  if (!response.aborted) {
    var resp = response.asJSON()
    for (var i=0;i<this.templates.length;i++) {
      this.templates[i].setData(resp[i]);
    }
  }
},

loadChickletsForSecondSlide: function() {          
  var r = new V$.ajax.Request("homepage.xdres", {
    onSuccess: this.onAjaxSuccess.v_bind(this)
  }); 
  r.send();
}

Related topics