.travis.yml: Add Node 4, remove iojs.
[mootools.git] / Docs / Request / Request.JSON.md
blobf9f40c67ba34da6dc0bee0f9316d0f844f2995aa
1 Class: Request.JSON {#Request-JSON}
2 =================================
4 Wrapped Request with automated receiving of JavaScript Objects in JSON Format.
6 ### Extends:
8 [Request](/core/Request/Request)
10 ### Syntax:
12         var myJSONRemote = new Request.JSON([options]);
14 ### Arguments:
16 1. options - (*object*, optional) See below.
18 ### Options:
20 * secure  - (*boolean*: defaults to true) If set to true, a syntax check will be done on the result JSON (see [JSON.decode](/Utilities/JSON#JSON:decode)).
22 ### Events:
24 #### success
26 Fired when the request completes. This overrides the signature of the Request success event.
28 ##### Signature:
30         onSuccess(responseJSON, responseText)
32 ##### Arguments:
34 1. responseJSON - (*object*) The JSON response object from the remote request.
35 2. responseText - (*string*) The JSON response as string.
37 #### error
39 Fired when the parsed JSON is not valid and the secure option is set.
41 ##### Signature:
43         onError(text, error)
45 ##### Arguments:
47 1. text - (string) The response text.
48 2. error - (string) The error message.
50 #### failure
52 Fired when the request failed (error status code).
54 ##### Signature:
56         onFailure(xhr)
58 ##### Arguments:
60 xhr - (XMLHttpRequest) The transport instance.
62 ### Returns:
64 * (*object*) A new Request.JSON instance.
66 ### Example:
68         // this code will send a data object via a GET request and alert the retrieved data.
69         var jsonRequest = new Request.JSON({url: 'http://site.com/tellMeAge.php', onSuccess: function(person){
70                 alert(person.age);    // alerts "25 years".
71                 alert(person.height); // alerts "170 cm".
72                 alert(person.weight); // alerts "120 kg".
73         }}).get({'firstName': 'John', 'lastName': 'Doe'});
75 ### Cross-Origin Resource Sharing (CORS) note:
77 The Request.JSON class will (by default) add two custom headers that, if used for a cross-origin request, will have to be reported as allowed in the preflight request, in addition to any other headers you may set yourself:
79         Access-Control-Allow-Headers: X-Requested-With, X-Request
81 ### See Also:
83 [Request](/core/Request/Request)