Ticket #16 (reopened enhancement)

Opened 3 years ago

Last modified 1 year ago

Support JSON-RPC

Reported by: ken@restivo.org Assigned to: somebody
Priority: normal Milestone: MochiKit 1.5
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

I tried to use MochiKit with the jsonrpc.py module that uses JSON-RPC.

I wanted to have interoperability with jsolait, which uses the JSON-RPC protocol. Others may want this too, or may want to migrate from jsolait to MochiKit.

The addition of the following two functions to MochiKit.Async seems to do the trick for me:

var postSimpleXMLHttpRequest = function (url, argdict) {

var self = MochiKit.Async; var req = self.getXMLHttpRequest(); req.open("POST", url, true); req.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); return self.sendXMLHttpRequest(req,argdict);

};

var postJSONDoc = function (url, argdict) {

var self = MochiKit.Async; var d = postSimpleXMLHttpRequest(url, serializeJSON(argdict)); d = d.addCallback(self.evalJSONRequest); return d;

};

I tested this with the jsonrpc.py module (from jsolait.net) and it interoperates just fine. Haven't beat it up with heavy testing, but so far "it works for me".

trypost = function(url, argdict) {

var d = postJSONDoc(url,argdict); d.addCallbacks(function(data){ writeln(inspect(data));}, function(err) { writeln(interpreterManager.showError(err))})

}

trypost('http://my/server/jsonrpc/testj.py', {'method':'echo',

'params': Yay! It works!?, 'id': 444});

Change History

12/04/05 09:07:01 changed by anonymous

  • status changed from new to closed.
  • resolution set to duplicate.

12/06/05 16:28:59 changed by bob@redivi.com

  • status changed from closed to reopened.
  • resolution deleted.

That's not really JSON RPC.. and I'm not sure that's quite the right way to implement it (e.g. the content-type for that post is totally wrong).

I'll look further into this soon, but for now you might want to take a look at http://trac.mochikit.com/wiki/MochiKitJsonRpc

12/13/06 17:39:59 changed by anonymous

Beter json-rpc

I'm developing Ajax aplication usgin Python and Mochikit and this is code for client json-rpc request.

/**
  * serialize obj as json string and send to the server using POST method
  * @return Deferred object
  */
var postJSON = function (url, obj) {
   var req = getXMLHttpRequest();
   req.open('POST', url, true);
   var header = function(key, value) {
      req.setRequestHeader(key, value);
   }
   header('Content-Type', 'application/json');
   header('Accept','application/json');
   var println = function(str) { 
      appendChildNodes('content', str + '\n');
   }
   var defer = sendXMLHttpRequest(req, serializeJSON(obj));
   defer.addCallback(evalJSONRequest);
   return defer;
};
/**
  * Call JSON-RPC method
  * @return Deferred object
  */
function jsonRPC(url, method, params, json_ok, json_error, xhr_error) {
  var request = {
     'version': '1.1',
     'id': Math.round(100*Math.random()),
     'method': method};
  if (params != null) {
    request['params'] = params
  }
  var d = postJSON(url, request);
  d.addCallback(function(data) {
     if (data.error == null) {
        json_ok(data);
     } else {
        json_error(data);
     }
  });
  if (xhr_error != null) {
     d.addErrback(xhr_error);
  }
  return d;
}

Example of use

var doc = currentDocument();
var println = function(str) {
    appendChildNodes(doc.body, str + '\n');
}
connect('foo', 'onclick', function(event) {
    jsonRPC('%s.json', 'get_date', null, 
        function(data) { println(data.result); },
        function(data) { println('error[json-rpc]: ' + data.error.error.name); },
        function(e) { println('error[XMLHttpRequest]: ' + e); }
    );
});

for third callback function in jsonRPC function check JSON-Specification

05/14/07 17:40:22 changed by anonymous

  • milestone set to MochiKit 1.5.

05/14/07 17:41:09 changed by rsayre@mozilla.com

I set that to 1.5