JsonRPC.js 1.2 KB

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