JsonP Documentation

Class: JsonP

Creates a Json request using script tag injection and handles the callbacks for you.

Author: Aaron Newton

Implements

Syntax

new JsonP(url[, options]);
 

Arguments

  1. url - (url) the url to get the json data
  2. options - (object) an object with key/value options

Options

  • callBackKey - (string) the key in the url that the server uses to wrap the Json results. So, for example, if you used callBackKey: 'callback' then the server is expecting something like http://..../?q=search+term&callback=myFunction; defaults to "callback". This must be defined correctly.
  • queryString - (string, optional) additional querystring values to append to the url
  • data - (object, optional) additional key/value data to append to the url

Events

  • onComplete - (function, optional) callback to execute when the data returns; it will be passed the data and the instance of jsonp that requested it.

Example

new JsonP('http://api.cnet.com/restApi/v1.0/techProductSearch', {
    data: {
        partTag: 'mtvo',
        iod: 'hlPrice',
        iewType: 'json',
        results: '100',
        query: 'ipod'
    },
    onComplete: myFunction.bind(someObject)
}).request();
 

The above example would generate this url:

http://api.cnet.com/restApi/v1.0/techProductSearch?partTag=mtvo&iod=hlPrice&viewType=json&results=100&query=ipod&callback=JsonP.requestors[0].handleResults&
 

It would embed a script tag (in the head of the document) with this url and, when it loaded, execute the "myFunction" callback defined.

JsonP Method: request

Executes the Json request.

Syntax

myJsonP.request();
 

Returns

  • (object) This instance of JsonP