source: Dev/trunk/src/client/dojox/rpc/JsonRPC.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 1.4 KB
Line 
1define("dojox/rpc/JsonRPC", ["dojo", "dojox", "dojox/rpc/Service", "dojo/errors/RequestError"], function(dojo, dojox, Service, RequestError) {
2
3        function jsonRpcEnvelope(version){
4                return {
5                        serialize: function(smd, method, data, options){
6                                //not converted to json it self. This  will be done, if
7                                //appropriate, at the transport level
8       
9                                var d = {
10                                        id: this._requestId++,
11                                        method: method.name,
12                                        params: data
13                                };
14                                if(version){
15                                        d.jsonrpc = version;
16                                }
17                                return {
18                                        data: dojo.toJson(d),
19                                        handleAs:'json',
20                                        contentType: 'application/json',
21                                        transport:"POST"
22                                };
23                        },
24       
25                        deserialize: function(obj){
26                                if ('Error' == obj.name // old xhr
27                                        || obj instanceof RequestError // new xhr
28                                ){
29                                        obj = dojo.fromJson(obj.responseText);
30                                }
31                                if(obj.error) {
32                                        var e = new Error(obj.error.message || obj.error);
33                                        e._rpcErrorObject = obj.error;
34                                        return e;
35                                }
36                                return obj.result;
37                        }
38                };
39        }
40        dojox.rpc.envelopeRegistry.register(
41                "JSON-RPC-1.0",
42                function(str){
43                        return str == "JSON-RPC-1.0";
44                },
45                dojo.mixin({namedParams:false}, jsonRpcEnvelope()) // 1.0 will only work with ordered params
46        );
47
48        dojox.rpc.envelopeRegistry.register(
49                "JSON-RPC-2.0",
50                function(str){
51                        return str == "JSON-RPC-2.0";
52                },
53                dojo.mixin({namedParams:true }, jsonRpcEnvelope("2.0")) // 2.0 supports named params
54        );
55
56});
Note: See TracBrowser for help on using the repository browser.