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 { /// /// 未帶參數 /// /// [JsonRpcMethod("getstring1")] public string getstring1() { return String.Format("Welcome to Jayrock by {0}!",DateTime.Now ); } /// /// 帶參數 /// ///The string. /// [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 getEntityList(string where) { return new Entity("demo").getEntityList(where); } }}
前台調用頁面
<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>