2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo._hasResource["dojo.rpc.JsonService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.rpc.JsonService"] = true;
10 dojo.provide("dojo.rpc.JsonService");
11 dojo.require("dojo.rpc.RpcService");
13 dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, {
15 contentType: "application/json-rpc",
18 callRemote: function(method, params){
20 // call an arbitrary remote method without requiring it to be
21 // predefined with SMD
23 // the name of the remote method you want to call.
25 // array of parameters to pass to method
27 var deferred = new dojo.Deferred();
28 this.bind(method, params, deferred);
32 bind: function(method, parameters, deferredRequestHandler, url){
34 // JSON-RPC bind method. Takes remote method, parameters,
35 // deferred, and a url, calls createRequest to make a JSON-RPC
36 // envelope and passes that off with bind.
38 // The name of the method we are calling
40 // The parameters we are passing off to the method
41 // deferredRequestHandler: deferred
42 // The Deferred object for this particular request
44 var def = dojo.rawXhrPost({
45 url: url||this.serviceUrl,
46 postData: this.createRequest(method, parameters),
47 contentType: this.contentType,
48 timeout: this.timeout,
49 handleAs: "json-comment-optional"
51 def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
54 createRequest: function(method, params){
56 // create a JSON-RPC envelope for the request
58 // The name of the method we are creating the requst for
60 // The array of parameters for this request;
62 var req = { "params": params, "method": method, "id": ++this.lastSubmissionId };
63 var data = dojo.toJson(req);
67 parseResults: function(/*anything*/obj){
69 // parse the result envelope and pass the results back to
70 // the callback function
72 // Object containing envelope of data we recieve from the server
74 if(dojo.isObject(obj)){
81 if("ResultSet" in obj){