- uploading 1.1 in tags
[mootools.git] / Remote / Json.Remote.js
blob97145a69b156e088929f5bb41ccd1a778c8052bc
1 /*
2 Script: Json.Remote.js
3         Contains <Json.Remote>.
5 License:
6         MIT-style license.
7 */
9 /*
10 Class: Json.Remote
11         Wrapped XHR with automated sending and receiving of Javascript Objects in Json Format.
13 Arguments:
14         url - the url you want to send your object to.
15         options - see <XHR> options
17 Example:
18         this code will send user information based on name/last name
19         (start code)
20         var jSonRequest = new Json.Remote("http://site.com/tellMeAge.php", {onComplete: function(person){
21                 alert(person.age); //is 25 years
22                 alert(person.height); //is 170 cm
23                 alert(person.weight); //is 120 kg
24         }}).send({'name': 'John', 'lastName': 'Doe'});
25         (end)
28 Json.Remote = XHR.extend({
30         initialize: function(url, options){
31                 this.url = url;
32                 this.addEvent('onSuccess', this.onComplete);
33                 this.parent(options);
34                 this.setHeader('X-Request', 'JSON');
35         },
37         send: function(obj){
38                 return this.parent(this.url, 'json=' + Json.toString(obj));
39         },
41         onComplete: function(){
42                 this.fireEvent('onComplete', Json.evaluate(this.response.text, this.options.secure));
43         }
45 });