JsonRPC.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright (c) 2004-2012, 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
  5. */
  6. if(!dojo._hasResource["dojox.rpc.JsonRPC"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.rpc.JsonRPC"] = true;
  8. dojo.provide("dojox.rpc.JsonRPC");
  9. dojo.require("dojox.rpc.Service");
  10. function jsonRpcEnvelope(version){
  11. return {
  12. serialize: function(smd, method, data, options){
  13. //not converted to json it self. This will be done, if
  14. //appropriate, at the transport level
  15. var d = {
  16. id: this._requestId++,
  17. method: method.name,
  18. params: data
  19. };
  20. if(version){
  21. d.jsonrpc = version;
  22. }
  23. return {
  24. data: dojo.toJson(d),
  25. handleAs:'json',
  26. contentType: 'application/json',
  27. transport:"POST"
  28. };
  29. },
  30. deserialize: function(obj){
  31. if ('Error' == obj.name){
  32. obj = dojo.fromJson(obj.responseText);
  33. }
  34. if(obj.error) {
  35. var e = new Error(obj.error.message || obj.error);
  36. e._rpcErrorObject = obj.error;
  37. return e;
  38. }
  39. return obj.result;
  40. }
  41. };
  42. }
  43. dojox.rpc.envelopeRegistry.register(
  44. "JSON-RPC-1.0",
  45. function(str){
  46. return str == "JSON-RPC-1.0";
  47. },
  48. dojo.mixin({namedParams:false},jsonRpcEnvelope()) // 1.0 will only work with ordered params
  49. );
  50. dojox.rpc.envelopeRegistry.register(
  51. "JSON-RPC-2.0",
  52. function(str){
  53. return str == "JSON-RPC-2.0";
  54. },
  55. jsonRpcEnvelope("2.0")
  56. );
  57. }