>>
I'd like to use jquery $.ajax to call a JsonRpcMethod instead of using
the proxy. I believe the jquery approach gives me some extra
capabilities that aren't covered by the proxy e.g. timeout, caching
etc.
Are my reasons valid for wanting to bypass the proxy and use jquery
instead or am I missing something?
<<
You don't need to bypass the proxy at all. Getting all the extra control and candy you desire is precisely why the proxy allows you to supply your own channel. A channel for the proxy is any object that supports a method named "rpc", accepting a single argument representing the call object. The call object is populated with various properties, like the URL of the service, remote method, parameters, local callback function and so forth. All you need to do is provide a jQuery-based channel. I've taken your original $.ajax call and re-wired it as a proxy channel (not much work):
function JQueryScriptChannel() {
this.rpc = function(call) {
if (call.request.params.constructor === Array)
throw new Error('Positional parameters are not supported.');
var params = [];
$.each(call.request.params, function(k, v) {
if (v) params.push(k + '=' + encodeURIComponent(v));
});
var ajax_REQ = $.ajax({
url: call.url + '/' + call.request.method,
type: 'GET',
cache: true,
data: params.join('&'),
dataType: 'json',
timeout: 10000,
// TODO error: ...
success: function(data) {
if (ajax_REQ) { ajax_REQ.abort(); }
call.callback(data);
}
});
}
}
Given the above, you can now setup a service proxy with the channel like this (DemoService below assumes you've sunk in the proxy for the demo service that's supplied with Jayrock):
var demo = new DemoService();
demo.channel = new JQueryScriptChannel();
demo.kwargs = true; // send args by name
And continue to make calls conveniently:
demo.sum(123, 456, function(response) {
alert(response.result); });
}
demo.echo('Jayrock + JQuery', function(response) {
alert(response.result); });
}
You can find a full version:
http://gist.github.com/7470
>>
Can somebody explain how I construct a URL to call a specific
JsonRpcMethod?
<<
The gory details are:
== Call Encoding Using HTTP GET ==
When using HTTP GET, the target procedure and parameters for the call are entirely expressed within the Request-URI of the HTTP message. The target procedure MUST appear as the last component of the Request-URI path component. The procedure's name MUST therefore be preceded by a forward-slash (U+002F or ASCII 47) but MUST NOT end in one.
The parameters are placed in the query component (as defined in RFC 3986) of the Request-URI, which is then formatted using the same scheme as defined for HTML Forms with the get method. Each parameters consists of a name/position and value pair that is separated by the equal sign (U+003D or ASCII 61) and parameters themselves are separated by an ampersand (U+0026 or ASCII 38):
name1=value1&name2=value2&name3=value3...
A client MAY send the parameters in a different order than in which the formal argument list of the target procedure defines them.
= Encoding of Call Parameter Values =
The client MUST NOT use the JSON syntax for the parameter values. All values MUST be plain text and observe the escaping rules defined for the query component of the URL. After decoding, the server MUST treat all values as if they were sent as JSON String values. The server MAY then perform conversions at its discretion (on a best-attempt basis) if the formal arguments of the target procedure expects other non-String values. This specification does not define any conversion rules or methods.
Parameters named identically on the query string MUST be collapsed into an Array of String values using the same order in which they appear in the query string and identified by the repeating parameter name. For instance, the following query string specifies two parameters only, namely scale and city:
city=london&scale=farenheit&city=zurich&city=new+york
The parameter scale has the single String value of "farenheit" whereas city is an Array of String values equivalent to the JSON text [ "london", "zurich", "new york" ].
It is specifically not possible to send parameters of type Object using HTTP GET.