Some notes I'd like to share. Comments and advice most welcome!
web2py supports JSONRPC 1.1 (and not 2.0 which is still a proposal).
This means there is no support for keyword arguments at this moment.
To work around this you can pass your methods a single argument which
is a dict, and parse the dict yourself. For example:
# in your controller
def mymethod(params):
param1 = params.get('key1', defaultvalue) # defaultvalue is optional
param2 = params.get('key2')
...
# do something with params and calculate result
return result
In your javascript code, send JSONRPC request as:
{"version": "1.1", "method": "mymethod",
"params": [{"key1": "val1", "key2": 2, ... "keyN": "valN"}],
id="someid"}
As you can see, the dict (object) is wrapped inside an array and array
has a single member.
Another note. You must include the id key. It can be anything, but
it's important to check that the same id is returned from the server.
I'm not sure if this prevents any security holes (I guess not), but at
least it doesn't open any new ones if you check it. So the whole
boilerplate for handling response would be:
if (response.id == idKey && response.error == null) {
callback(response);
}
else {
DEBUG('JSONRPC error');
errback(response);
}
The ``idKey`` variable is some random key I generate before every
JSONRPC call, and I check that the response contains the same key.
``response.error`` is a non-null value if there is an error, so it's
also good to check that before firing the callback.
--
Branko Vukelić
[email protected]
[email protected]
Check out my blog: http://www.brankovukelic.com/
Check out my portfolio: http://www.flickr.com/photos/foxbunny/
Registered Linux user #438078 (http://counter.li.org/)
I hang out on identi.ca: http://identi.ca/foxbunny
Gimp Brushmakers Guild
http://bit.ly/gbg-group