mojoPortal 项目蒙运用Joyrock和MagicAjaxNET,他不曾动Asp.net ajax
,是因mojoPortal是一个运转在Windows的 .net framework或Linux,Mac
OS的Mono平台上之cms系统,asp.net ajax
的商决定了其不能够应用被mono上。Joyrock的求实应用得去看mojoPortal
的流行版本的代码。

Joyrock大凡一个LGPL的开源之软件,实现了JSON和JSON-RPC,支持微软ASP.NET框架。

探望服务器端的写法:
<%@ WebHandler Class=”JayrockWeb.HelloWorld” %>

namespace JayrockWeb
{
    using System;
    using System.Web;
    using Jayrock.Json;
    using Jayrock.JsonRpc;
    using Jayrock.JsonRpc.Web;

    public class HelloWorld : JsonRpcHandler
    {
        [ JsonRpcMethod(“greetings”) ]
        public string Greetings()
        {
            return “Welcome to Jayrock!”;
        }
    }
}
[ JsonRpcMethod(“greetings”) ]刚刚对应于ASP.NET
的[WebMethod],深入理解一下即便知晓,这半独自定义属性就是起及号作用,用来声称方法是得远距离调用的。

客户端调用:
 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en”
xml:lang=”en”>
<head>
    <title>Hello Jayrock</title>
    <script type=”text/javascript” src=”json.js”></script>
    <script type=”text/javascript”
src=”helloworld.ashx?proxy”></script>
    <script type=”text/javascript”>
/* <![CDATA[ */

window.onload = function()
{
    var s = new HelloWorld();

    alert(“sync:” + s.greetings());

    s.greetings(function(response) {
      alert(“async:” + response.result)
    });
}

/* ]]> */
    </script>
</head>
<body>
    <p>This page tests the HelloWorld service with
Jayrock.</p>
</body>

足见到helloworld.ashx?proxy指于了一个JS文件,他的内容就是:
// This JavaScript was automatically generated by
// Jayrock.Json.Rpc.Web.JsonRpcProxyGenerator, Jayrock,
Version=0.9.7507.0, Culture=neutral, PublicKeyToken=null
// on 2006年12月5日 at 8:46:27 (中国标准日)

// Proxy version 1.0

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对象吃之参数信息,确定要的服务器端方法名称和参数,然后进行调用,并回到结果。

相关文章

网站地图xml地图