ASP.net Joyrock异步应用示例、JSON-RPC使用方法
使用Joyrock 需要先引用Jayrock.dll,Jayrock.Json.dll两文件,另外在web页面引入json.js文件(不在同一个地方下载的json.js可用不了),
.net 后台文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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. /// <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); } }}</jsonobject> |
前台调用页面
|
1
|
|
<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>
转载 :http://www.2cto.com/kf/201505/401062.html

浙公网安备 33010602011771号