Ajax的远程调用(2)

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_r(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 == 4)
        {
            var response = sender.status == 200 ?
                JSON.eval_r(sender.responseText) : {};
           
            response.xmlHTTP = sender;
               
            callback(response);
        }
    }

function newHTTP()
    {
        return typeof(ActiveXObject) === 'function' ?
            new ActiveXObject('Microsoft.XMLHTTP') :
            new XMLHttpRequest();
    }
}

HelloWorld.rpcMethods = ["system.about","system.version","system.listMethods","greetings"];


上面JS文档是自动生成的,ASP.NET AJAX也有自动生成客户端访问对象的功能,方法不得而知,而Jayrock的方法确实清晰的,因为有Source Code。
Jayrock 远程方法要求写在一个ashx中,页面请求这个ashx的时候,在ProcessRequest 中根据Request对象中的参数信息,确定请求的服务器端方法名称和参数,然后进行调用,并返回结果。
Jayrock 功能完善,代码比较长,下面是个原理实例,并非Jayrock的Source Code。
<%@ WebHandler Language="C#" %>

using System;
using System.Web;
using MyRpc;

public class HelloWorld : IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {
        string oMethod = context.Request.Params[0];
   Type t = this.GetType();
        System.Reflection.MethodInfo []mis = t.GetMethods();

foreach (System.Reflection.MethodInfo mi in mis)
        {
            foreach (MyRpcServiceAttribute ma in mi.GetCustomAttributes(typeof(MyRpcServiceAttribute), false))
            {
                string strResult = "";
                if (mi.Name == oMethod)
                {
                    if (!mi.IsStatic)
                    {
                        object oService = Activator.CreateInstance(t);
                        strResult = (string)mi.Invoke(oService, null);
                    }
                    else
                    {
                        strResult = (string)mi.Invoke(null, null);
                    }
                }
                context.Response.Write(strResult);
               
            }
           
        }   

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11454.html