Sending JSON-RPC 2.0 requests with jQuery

Dispatching JSON-RPC 2.0 – style requests with jQuery is easy. Just use the jQuery.post() method:

var url = "http://rpc-service.net/jsonrpc2/";

var request = {};
request.method = "ldap.search";
request.params = {};
request.params.CID = "45d0677d-a336-463b-ad99-c82137d03a00";
request.params.baseDN = "ou=people,dc=example,dc=com";
request.params.scope = "ONE";
request.params.filter = "(givenName=John)";
request.id = 1;
request.jsonrpc = "2.0";

function displaySearchResult(response) {

        if (response.result)
                alert(response.result);

        else if (response.error)
                alert("Search error: " + response.error.message);
};

$.post(url, JSON.stringify(request), displaySearchResult, "json");

The above example assumes that you have jQuery 1.4+ and the json2 encoder/decoder installed.

For those of you who are new to JSON-RPC 2.0 – this is the second release of the simple remote-procedure call (RPC) protocol using JSON messages. I’ve incorporated it in a number of my own software products, the idea being that it would be better for all of us – customers and developers alike – to standardise on a common JSON protocol for web services, instead of every one coming up with their own ad-hoc JSON protocol. I’ve spun off my minimalist Java implementation into a separate client/server library.

The JSON-RPC community has its forum at Google groups: http://groups.google.com/group/json-rpc