function HelloWorld(url)
{
/* Returns a summary about the JSON-RPC server implementation for display purposes. */
this["system.about"] = function(callback)
{
return call("system.about", [ ], callback);
}
/* Returns the version JSON-RPC server implementation using the major, minor, build and revision format. */
this["system.version"] = function(callback)
{
return call("system.version", [ ], callback);
}
/* Returns an array of method names implemented by this service. */
this["system.listMethods"] = function(callback)
{
return call("system.listMethods", [ ], callback);
}
this["greetings"] = function(callback)
{
return call("greetings", [ ], callback);
}
var url = typeof(url) === 'string' ? url : 'http://localhost:3409/Web/HelloWorld.ashx';
var self = this;
var nextId = 0;
function call(method, params, callback)
{
var request = { id : nextId++, method : method, params : params };
return callback == null ?
callSync(method, request) : callAsync(method, request, callback);
}
function callSync(method, request)
{
var http = newHTTP();
http.open('POST', url, false, self.httpUserName, self.httpPassword);
setupHeaders(http, method);
http.send(JSON.stringify(request));
if (http.status != 200)
throw { message : http.status + ' ' + http.statusText, toString : function() { return message; } };
var response = JSON.eval(http.responseText);
if (response.error != null) throw response.error;
return response.result;
}
function callAsync(method, request, callback)
{
var http = newHTTP();
http.open('POST', url, true, self.httpUserName, self.httpPassword);
setupHeaders(http, method);
http.onreadystatechange = function() { http_onreadystatechange(http, callback); }
http.send(JSON.stringify(request));
return request.id;
}
function setupHeaders(http, method)
{
http.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
http.setRequestHeader('X-JSON-RPC', method);
}
function http_onreadystatechange(sender, callback)
{
if (sender.readyState == /* complete */ 4)
{
var response = sender.status == 200 ?
JSON.eval(sender.responseText) : {};
response.xmlHTTP = sender;
callback(response);
}
}
function newHTTP()
{
return typeof(ActiveXObject) === 'function' ?
new ActiveXObject('Microsoft.XMLHTTP') : /* IE 5 */
new XMLHttpRequest(); /* Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */
}
}
HelloWorld.rpcMethods = ["system.about","system.version","system.listMethods","greetings"];
上面JS文档是自动生成的,ASP.NET AJAX也有自动生成客户端访问对象的功能
Jayrock 远程方法要求写在一个ashx中,页面请求这个ashx的时候,在ProcessRequest 中根据Request对象中的参数信息,确定请求的服务器端方法名称和参数,然后进行调用,并返回结果。