ASP.net Joyrock异步应用示例、JSON-RPC使用方法

使用Joyrock 需要先引用Jayrock.dll,Jayrock.Json.dll两文件,另外在web页面引入json.js文件(不在同一个地方下载的json.js可用不了),

.net 后台文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

namespace Exam.Jayrock
{

    public class Handler1 : JsonRpcHandler
    {
        /// <summary>
        /// 未带参数
        /// </summary>
        /// <returns></returns>
        [JsonRpcMethod("getstring1")]
        public string getstring1()
        {
            return String.Format("Welcome to Jayrock by {0}!",DateTime.Now );
        }
        /// <summary>
        /// 带参数
        /// </summary>
        /// <param Name="str">The string.</param>
        /// <returns></returns>
        [JsonRpcMethod("getstring2")]
        public string getstring2(string str)
        {
            return String.Format("Welcome to Jayrock by {0}!", str);
        }
        [JsonRpcMethod("addEntity")]
        public int addEntity(JsonObject ent)
        {
            new Entity("demo").addEntity(ent);
            return 1;
        }
        [JsonRpcMethod("updateEntity")]
        public int updateEntity(JsonObject ent) 
        {
            new Entity("demo").updateEntity(ent);
            return 1;
        }
        [JsonRpcMethod("delEntity")]
        public int delEntity(int id)
        {
            new Entity("demo").delEntity(id);
            return 1;
        }
        [JsonRpcMethod("getEntityList")]
        public List<JsonObject> getEntityList(string where)
        {
            return new Entity("demo").getEntityList(where);
        }
    }
}

前台调用页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>demo</title>
    <script src="json.js" type="text/javascript"></script>
    <script type="text/javascript" src="Handler1.ashx?proxy"></script>
    <script>
        var demo = new Handler1();
        
        function syncGetString1()
        {
            alert(demo.getstring1());
        }
        function asyncGetString1()
        {
            demo.getstring1(function (val){
                alert(val.result);
            })
        }
        function syncGetString2()
        {
            var str = "voodooer";
            alert(demo.getstring2(str));
        }
        function asyncGetString2()
        {
            var str = "voodooer";
            demo.getstring2(str,function (val)
            {
                alert(val.result);
            })
            alert("我执行完了:)");
        }
        function addEntity()
        {
            var Entity = Object();
            Entity.Name = "voodoer";
            Entity.Sex = "男";
            var result = demo.addEntity(Entity);
            if (result > 0) { alert("添加实体成功!");}
        }
        function updateEntity()
        {
            var Entity = Object();
            Entity.ID = 1;
            Entity.Name = "voodoer";
            Entity.Sex = "男";
            var result = demo.updateEntity(Entity);
            if (result > 0) { alert("更新实体成功!"); }
        }
        function delEntity()
        {
            
            var result = demo.delEntity(1);
            if (result > 0) { alert("删除实体成功!"); }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>测试getstring1</p>
        <input type="button" id="btn1"  value="同步调用" onclick="syncGetString1()"/>
        <input type="button" id="btn2"  value="异步调用" onclick="asyncGetString1()"/>
        <hr />
        <p>测试getstring2</p>
        <input type="button" id="Button1"  value="同步调用" onclick="syncGetString2()"/>
        <input type="button" id="Button2"  value="异步调用" onclick="asyncGetString2()"/>
        <hr />
        <p>测试Entity</p>
        <input type="button" id="Button3"  value="addEntity" onclick="addEntity()"/>
        <input type="button" id="Button4"  value="updateEntity" onclick="updateEntity()"/>
        <input type="button" id="Button5"  value="delEntity" onclick="delEntity()"/>
        <hr />
    </div>
    </form>
</body>
</html>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。