基于Java的Ajax框架之JSON-RPC(二)

本文介绍了一种使用 JSON-RPC 协议实现客户端与服务器间交互的方法。通过示例展示了如何配置服务器端 JSONRPCServlet 以及客户端如何通过 JSONRPCBridge 进行调用。具体包括 web.xml 的配置、TipService 类的设计、index.jsp 页面的实现及必要的 JavaScript 脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上例:http://blog.youkuaiyun.com/kunshan_shenbin/archive/2008/11/25/3368266.aspx

这次尝试通过映射服务器端JSONRPCServlet,编写处理类来实现和客户端的交互(客户端通过JSONRPCBridge类实现交互)。

代码:

TipService.java

  1. package demo;
  2. import java.io.Serializable;
  3. public class TipService implements Serializable
  4. {
  5.     private static final long serialVersionUID = -276869945037249233L;
  6.     
  7.     private String[][] tips= 
  8.     {
  9.         {"1" , "IBM笔记本电脑,价格有点贵,<br>外观保守,适合稳重型用户。"},
  10.         {"2" , "Dell笔记本电脑,价格适中,<br>兼顾中、低端用户,有更多的<br>选择余地。"},
  11.         {"3" , "SONY笔记本电脑,源自日本,<br>细致工艺,优美的外观都是其<br>卖点,比较贵。"}
  12.     };
  13.     public String getTip(int id)
  14.     {
  15.         for (String[] tip : tips )
  16.         {
  17.             if (tip[0].equals(String.valueOf(id)))
  18.             {
  19.                 return tip[1];
  20.             }
  21.         }
  22.         return null;
  23.     }
  24. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6.     <servlet>
  7.         <servlet-name>jsonServlet</servlet-name>
  8.         <servlet-class>
  9.             com.metaparadigm.jsonrpc.JSONRPCServlet
  10.         </servlet-class>
  11.     </servlet>
  12.     <!-- 此处的url-pattern可以随便更改,只是JavaScript代码中JSONRpcClient以此地址创建 -->
  13.     <servlet-mapping>
  14.         <servlet-name>jsonServlet</servlet-name>
  15.         <url-pattern>/myjson</url-pattern>
  16.     </servlet-mapping>
  17.     <welcome-file-list>
  18.         <welcome-file>index.jsp</welcome-file>
  19.     </welcome-file-list>
  20.     
  21. </web-app>

index.jsp

  1. <%@ page contentType="text/html;charset=utf-8"%>
  2. <jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
  3. <jsp:useBean id="tip" scope="session" class="demo.TipService" />
  4. <% JSONRPCBridge.registerObject("tip", tip); %>
  5. <HTML>
  6. <HEAD>
  7. <TITLE>在线商场</TITLE>
  8. <META http-equiv=Content-Type content="text/html; charset=utf-8">
  9. </HEAD>
  10.     <script type="text/javascript" src="jsonrpc.js"></script>
  11.     <script type="text/javascript" src="tip.js"></script>
  12. <BODY topMargin=20 onLoad="onLoad()">
  13. <h3 align="center">请挑选您想购买的笔记本</h3>
  14. <table width="640" height="208" border="1" align="center" style="border-collapse:collapse">
  15.   <tr>
  16.     <td width="230"><img id="1" src="img/ibm.jpg" onMouseOver="getTip(this.id , event);" onMouseOut="hideTip()"/></td>
  17.     <td width="400">
  18.     <table width="400" border="0" style="font-size:10pt">
  19.        <tr>
  20.         <td><h3 align="center">ThinkPad T60 2007GT1</h3></td>
  21.       </tr>
  22.       <tr>
  23.         <td><strong>处理器: </strong>Intel Core2 Duo(Merom) T7200(2.0G)</td>
  24.       </tr>   
  25.       <tr>
  26.         <td><strong>内存容量: </strong>512M</td>
  27.       </tr>
  28.       <tr>
  29.         <td><strong>硬盘容量: </strong>120G</td>
  30.       </tr>
  31.       <tr>
  32.         <td><strong>屏幕尺寸: </strong>14.1寸</td>
  33.       </tr>
  34.       <tr>
  35.         <td><strong>显示芯片: </strong>独立,ATI Mobility Radeon X1300</td>
  36.       </tr>
  37.       <tr>
  38.         <td><strong>重量: </strong>约2.32Kg</td>
  39.       </tr>
  40.     </table></td>
  41.   </tr>
  42.   <tr>
  43.     <td><img id="2" src="img/dell.jpg"  onMouseOver="getTip(this.id , event);" onMouseOut="hideTip()"/></td>
  44.     <td>
  45.     <table width="400" border="0" style="font-size:10pt">
  46.       <tr>
  47.         <td><h3 align="center">戴尔 Inspiron 6400(N511212)</h3></td>
  48.       </tr>
  49.       <tr>
  50.         <td><strong>处理器: </strong>Intel Core2 Duo(Merom) T5500(1.66G)</td>
  51.       </tr>   
  52.       <tr>
  53.         <td><strong>内存容量: </strong>512M</td>
  54.       </tr>
  55.       <tr>
  56.         <td><strong>硬盘容量: </strong>120G</td>
  57.       </tr>
  58.       <tr>
  59.         <td><strong>屏幕尺寸: </strong>14.1寸</td>
  60.       </tr>
  61.       <tr>
  62.         <td><strong>显示芯片: </strong>独立,ATI Mobility Radeon X1300</td>
  63.       </tr>
  64.       <tr>
  65.         <td><strong>重量: </strong>约2.32Kg</td>
  66.       </tr>
  67.     </table></td>
  68.   </tr>
  69.     <tr>
  70.     <td><img id="3" src="img/sony.jpg"  onMouseOver="getTip(this.id , event);" onMouseOut="hideTip()"/></td>
  71.     <td>
  72.     <table width="400" border="0" style="font-size:10pt">
  73.       <tr>
  74.         <td><h3 align="center">索尼 VGN-AR18CP</h3></td>
  75.       </tr>
  76.       <tr>
  77.         <td><strong>处理器: </strong>Intel Core Duo(Yonah) T2600(2.16G)</td>
  78.       </tr>
  79.       <tr>
  80.         <td><strong>内存容量: </strong>2048M</td>
  81.       </tr>
  82.       <tr>
  83.         <td><strong>硬盘容量: </strong>160G</td>
  84.       </tr>
  85.       <tr>
  86.         <td><strong>屏幕尺寸: </strong>17寸</td>
  87.       </tr>
  88.       <tr>
  89.         <td><strong>显示芯片: </strong>独立,nVidia Geforce Go7600GT</td>
  90.       </tr>
  91.       <tr>
  92.         <td><strong>重量: </strong>约3.8Kg</td>
  93.       </tr> 
  94.     </table>
  95.     </td>
  96.   </tr>
  97. </table>
  98. <div id="tip" style="position:absolute; z-index:100;font-size:10pt;background-color:#eeeeaa;border:1px solid #999999" >
  99. </div>
  100. </BODY>
  101. </HTML>

jsonrpc.js(在json-rpc-java-1.0.1.zip包中可以找到,下载地址:http://oss.metaparadigm.com/jsonrpc/download.html

  1. /*
  2.  * JSON-RPC JavaScript client
  3.  *
  4.  * $Id: jsonrpc.js,v 1.36.2.3 2006/03/08 15:09:37 mclark Exp $
  5.  *
  6.  * Copyright (c) 2003-2004 Jan-Klaas Kollhof
  7.  * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
  8.  *
  9.  * This code is based on Jan-Klaas' JavaScript o lait library (jsolait).
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License");
  12.  * you may not use this file except in compliance with the License.
  13.  * You may obtain a copy of the License at
  14.  *
  15.  *    http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software
  18.  * distributed under the License is distributed on an "AS IS" BASIS,
  19.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20.  * See the License for the specific language governing permissions and
  21.  * limitations under the License.
  22.  *
  23.  */
  24. /* escape a character */
  25. escapeJSONChar =
  26. function escapeJSONChar(c)
  27. {
  28.     if(c == "/"" || c == "//") return "//" + c;
  29.     else if (c == "/b"return "//b";
  30.     else if (c == "/f"return "//f";
  31.     else if (c == "/n"return "//n";
  32.     else if (c == "/r"return "//r";
  33.     else if (c == "/t"return "//t";
  34.     var hex = c.charCodeAt(0).toString(16);
  35.     if(hex.length == 1) return "//u000" + hex;
  36.     else if(hex.length == 2) return "//u00" + hex;
  37.     else if(hex.length == 3) return "//u0" + hex;
  38.     else return "//u" + hex;
  39. };
  40. /* encode a string into JSON format */
  41. escapeJSONString =
  42. function escapeJSONString(s)
  43. {
  44.     /* The following should suffice but Safari's regex is b0rken
  45.        (doesn't support callback substitutions)
  46.        return "/"" + s.replace(/([^/u0020-/u007f]|[///"])/g,
  47.        escapeJSONChar) + "/"";
  48.     */
  49.     /* Rather inefficient way to do it */
  50.     var parts = s.split("");
  51.     for(var i=0; i < parts.length; i++) {
  52.     var c =parts[i];
  53.     if(c == '"' ||
  54.        c == '//' ||
  55.        c.charCodeAt(0) < 32 ||
  56.        c.charCodeAt(0) >= 128)
  57.         parts[i] = escapeJSONChar(parts[i]);
  58.     }
  59.     return "/"" + parts.join("") + "/"";
  60. };
  61. /* Marshall objects to JSON format */
  62. toJSON = function toJSON(o)
  63. {
  64.     if(o == null) {
  65.     return "null";
  66.     } else if(o.constructor == String) {
  67.     return escapeJSONString(o);
  68.     } else if(o.constructor == Number) {
  69.     return o.toString();
  70.     } else if(o.constructor == Boolean) {
  71.     return o.toString();
  72.     } else if(o.constructor == Date) {
  73.     return '{javaClass: "java.util.Date", time: ' + o.valueOf() +'}';
  74.     } else if(o.constructor == Array) {
  75.     var v = [];
  76.     for(var i = 0; i < o.length; i++) v.push(toJSON(o[i]));
  77.     return "[" + v.join(", ") + "]";
  78.     } else {
  79.     var v = [];
  80.     for(attr in o) {
  81.         if(o[attr] == null) v.push("/"" + attr + "/": null");
  82.         else if(typeof o[attr] == "function"); /* skip */
  83.         else v.push(escapeJSONString(attr) + ": " + toJSON(o[attr]));
  84.     }
  85.     return "{" + v.join(", ") + "}";
  86.     }
  87. };
  88. /* JSONRpcClient constructor */
  89. JSONRpcClient =
  90. function JSONRpcClient_ctor(serverURL, user, pass, objectID)
  91. {
  92.     this.serverURL = serverURL;
  93.     this.user = user;
  94.     this.pass = pass;
  95.     this.objectID = objectID;
  96.     /* Add standard methods */
  97.     if(this.objectID) {
  98.     this._addMethods(["listMethods"]);
  99.     var req = this._makeRequest("listMethods", []);
  100.     } else {
  101.     this._addMethods(["system.listMethods"]);
  102.     var req = this._makeRequest("system.listMethods", []);
  103.     }
  104.     var m = this._sendRequest(req);
  105.     this._addMethods(m);
  106. };
  107. /* JSONRpcCLient.Exception */
  108. JSONRpcClient.Exception =
  109. function JSONRpcClient_Exception_ctor(code, message, javaStack)
  110. {
  111.     this.code = code;
  112.     var name;
  113.     if(javaStack) {
  114.     this.javaStack = javaStack;
  115.     var m = javaStack.match(/^([^:]*)/);
  116.     if(m) name = m[0];
  117.     }
  118.     if(name) this.name = name;
  119.     else this.name = "JSONRpcClientException";
  120.     this.message = message;
  121. };
  122. JSONRpcClient.Exception.CODE_REMOTE_EXCEPTION = 490;
  123. JSONRpcClient.Exception.CODE_ERR_CLIENT = 550;
  124. JSONRpcClient.Exception.CODE_ERR_PARSE = 590;
  125. JSONRpcClient.Exception.CODE_ERR_NOMETHOD = 591;
  126. JSONRpcClient.Exception.CODE_ERR_UNMARSHALL = 592;
  127. JSONRpcClient.Exception.CODE_ERR_MARSHALL = 593;
  128. JSONRpcClient.Exception.prototype = new Error();
  129. JSONRpcClient.Exception.prototype.toString =
  130. function JSONRpcClient_Exception_toString(code, msg)
  131. {
  132.     return this.name + ": " + this.message;
  133. };
  134. /* Default top level exception handler */
  135. JSONRpcClient.default_ex_handler =
  136. function JSONRpcClient_default_ex_handler(e) { alert(e); };
  137. /* Client settable variables */
  138. JSONRpcClient.toplevel_ex_handler = JSONRpcClient.default_ex_handler;
  139. JSONRpcClient.profile_async = false;
  140. JSONRpcClient.max_req_active = 1;
  141. JSONRpcClient.requestId = 1;
  142. /* JSONRpcClient implementation */
  143. JSONRpcClient.prototype._createMethod =
  144. function JSONRpcClient_createMethod(methodName)
  145. {
  146.     var fn=function()
  147.     {
  148.     var args = [];
  149.     var callback = null;
  150.     for(var i=0;i<arguments.length;i++) args.push(arguments[i]);
  151.     if(typeof args[0] == "function") callback = args.shift();
  152.     var req = fn.client._makeRequest.call(fn.client, fn.methodName,
  153.                           args, callback);
  154.     if(callback == null) {
  155.         return fn.client._sendRequest.call(fn.client, req);
  156.     } else {
  157.         JSONRpcClient.async_requests.push(req);
  158.         JSONRpcClient.kick_async();
  159.         return req.requestId;
  160.     }
  161.     };
  162.     fn.client = this;
  163.     fn.methodName = methodName;
  164.     return fn;
  165. };
  166. JSONRpcClient.prototype._addMethods =
  167. function JSONRpcClient_addMethods(methodNames)
  168. {
  169.     for(var i=0; i<methodNames.length; i++) {
  170.     var obj = this;
  171.     var names = methodNames[i].split(".");
  172.     for(var n=0; n<names.length-1; n++) {
  173.         var name = names[n];
  174.         if(obj[name]) {
  175.         obj = obj[name];
  176.         } else {
  177.         obj[name]  = new Object();
  178.         obj = obj[name];
  179.         }
  180.     }
  181.     var name = names[names.length-1];
  182.     if(!obj[name]) {
  183.         var method = this._createMethod(methodNames[i]);
  184.         obj[name] = method;
  185.     }
  186.     }
  187. };
  188. JSONRpcClient._getCharsetFromHeaders =
  189. function JSONRpcClient_getCharsetFromHeaders(http)
  190. {
  191.     try {
  192.     var contentType = http.getResponseHeader("Content-type");
  193.     var parts = contentType.split(//s*;/s*/);
  194.     for(var i =0; i < parts.length; i++) {
  195.         if(parts[i].substring(0, 8) == "charset=")
  196.         return parts[i].substring(8, parts[i].length);
  197.     }
  198.     } catch (e) {}
  199.     return "UTF-8"/* default */
  200. };
  201. /* Async queue globals */
  202. JSONRpcClient.async_requests = [];
  203. JSONRpcClient.async_inflight = {};
  204. JSONRpcClient.async_responses = [];
  205. JSONRpcClient.async_timeout = null;
  206. JSONRpcClient.num_req_active = 0;
  207. JSONRpcClient._async_handler =
  208. function JSONRpcClient_async_handler()
  209. {
  210.     JSONRpcClient.async_timeout = null;
  211.     while(JSONRpcClient.async_responses.length > 0) {
  212.     var res = JSONRpcClient.async_responses.shift();
  213.     if(res.canceled) continue;
  214.     if(res.profile) res.profile.dispatch = new Date();
  215.     try {
  216.         res.cb(res.result, res.ex, res.profile);
  217.     } catch(e) {
  218.         JSONRpcClient.toplevel_ex_handler(e);
  219.     }
  220.     }
  221.     while(JSONRpcClient.async_requests.length > 0 &
  222.       JSONRpcClient.num_req_active < JSONRpcClient.max_req_active) {
  223.     var req = JSONRpcClient.async_requests.shift();
  224.     if(req.canceled) continue;
  225.     req.client._sendRequest.call(req.client, req);
  226.     }
  227. };
  228. JSONRpcClient.kick_async =
  229. function JSONRpcClient_kick_async()
  230. {
  231.     if(JSONRpcClient.async_timeout == null)
  232.     JSONRpcClient.async_timeout =
  233.         setTimeout(JSONRpcClient._async_handler, 0);
  234. };
  235. JSONRpcClient.cancelRequest =
  236. function JSONRpcClient_cancelRequest(requestId)
  237. {
  238.     /* If it is in flight then mark it as canceled in the inflight map
  239.        and the XMLHttpRequest callback will discard the reply. */
  240.     if(JSONRpcClient.async_inflight[requestId]) {
  241.     JSONRpcClient.async_inflight[requestId].canceled = true;
  242.     return true;
  243.     }
  244.     /* If its not in flight yet then we can just mark it as canceled in
  245.        the the request queue and it will get discarded before being sent. */
  246.     for(var i in JSONRpcClient.async_requests) {
  247.     if(JSONRpcClient.async_requests[i].requestId == requestId) {
  248.         JSONRpcClient.async_requests[i].canceled = true;
  249.         return true;
  250.     }
  251.     }
  252.     /* It may have returned from the network and be waiting for its callback
  253.        to be dispatched, so mark it as canceled in the response queue
  254.        and the response will get discarded before calling the callback. */
  255.     for(var i in JSONRpcClient.async_responses) {
  256.     if(JSONRpcClient.async_responses[i].requestId == requestId) {
  257.         JSONRpcClient.async_responses[i].canceled = true;
  258.         return true;
  259.     }
  260.     }
  261.     return false;
  262. };
  263. JSONRpcClient.prototype._makeRequest =
  264. function JSONRpcClient_makeRequest(methodName, args, cb)
  265. {
  266.     var req = {};
  267.     req.client = this;
  268.     req.requestId = JSONRpcClient.requestId++;
  269.     var obj = {};
  270.     obj.id = req.requestId;
  271.     if (this.objectID)
  272.     obj.method = ".obj#" + this.objectID + "." + methodName;
  273.     else
  274.     obj.method = methodName;
  275.     obj.params = args;
  276.     if (cb) req.cb = cb;
  277.     if (JSONRpcClient.profile_async)
  278.     req.profile = { "submit"new Date() };
  279.     req.data = toJSON(obj);
  280.     return req;
  281. };
  282. JSONRpcClient.prototype._sendRequest =
  283. function JSONRpcClient_sendRequest(req)
  284. {
  285.     if(req.profile) req.profile.start = new Date();
  286.     /* Get free http object from the pool */
  287.     var http = JSONRpcClient.poolGetHTTPRequest();
  288.     JSONRpcClient.num_req_active++;
  289.     /* Send the request */
  290.     if (typeof(this.user) == "undefined") {
  291.     http.open("POST"this.serverURL, (req.cb != null));
  292.     } else {
  293.     http.open("POST"this.serverURL, (req.cb != null), this.user, this.pass);
  294.     }
  295.     /* setRequestHeader is missing in Opera 8 Beta */
  296.     try { http.setRequestHeader("Content-type""text/plain"); } catch(e) {}
  297.     /* Construct call back if we have one */
  298.     if(req.cb) {
  299.     var self = this;
  300.     http.onreadystatechange = function() {
  301.         if(http.readyState == 4) {
  302.         http.onreadystatechange = function () {};
  303.         var res = { "cb": req.cb, "result"null"ex"null};
  304.         if (req.profile) {
  305.             res.profile = req.profile;
  306.             res.profile.end = new Date();
  307.         }
  308.         try { res.result = self._handleResponse(http); }
  309.         catch(e) { res.ex = e; }
  310.         if(!JSONRpcClient.async_inflight[req.requestId].canceled)
  311.             JSONRpcClient.async_responses.push(res);
  312.         delete JSONRpcClient.async_inflight[req.requestId];
  313.         JSONRpcClient.kick_async();
  314.         }
  315.     };
  316.     } else {
  317.     http.onreadystatechange = function() {};
  318.     }
  319.     JSONRpcClient.async_inflight[req.requestId] = req;
  320.     
  321.     try {
  322.     http.send(req.data);
  323.     } catch(e) {
  324.     JSONRpcClient.poolReturnHTTPRequest(http);
  325.     JSONRpcClient.num_req_active--;
  326.     throw new JSONRpcClient.Exception
  327.         (JSONRpcClient.Exception.CODE_ERR_CLIENT, "Connection failed");
  328.     }
  329.     if(!req.cb) return this._handleResponse(http);
  330. };
  331. JSONRpcClient.prototype._handleResponse =
  332. function JSONRpcClient_handleResponse(http)
  333. {
  334.     /* Get the charset */
  335.     if(!this.charset) {
  336.     this.charset = JSONRpcClient._getCharsetFromHeaders(http);
  337.     }
  338.     /* Get request results */
  339.     var status, statusText, data;
  340.     try {
  341.     status = http.status;
  342.     statusText = http.statusText;
  343.     data = http.responseText;
  344.     } catch(e) {
  345.     JSONRpcClient.poolReturnHTTPRequest(http);
  346.     JSONRpcClient.num_req_active--;
  347.     JSONRpcClient.kick_async();
  348.     throw new JSONRpcClient.Exception
  349.         (JSONRpcClient.Exception.CODE_ERR_CLIENT, "Connection failed");
  350.     }
  351.     /* Return http object to the pool; */
  352.     JSONRpcClient.poolReturnHTTPRequest(http);
  353.     JSONRpcClient.num_req_active--;
  354.     /* Unmarshall the response */
  355.     if(status != 200) {
  356.     throw new JSONRpcClient.Exception(status, statusText);
  357.     }
  358.     var obj;
  359.     try {
  360.     eval("obj = " + data);
  361.     } catch(e) {
  362.     throw new JSONRpcClient.Exception(550, "error parsing result");
  363.     }
  364.     if(obj.error)
  365.     throw new JSONRpcClient.Exception(obj.error.code, obj.error.msg,
  366.                       obj.error.trace);
  367.     var res = obj.result;
  368.     /* Handle CallableProxy */
  369.     if(res && res.objectID && res.JSONRPCType == "CallableReference")
  370.     return new JSONRpcClient(this.serverURL, this.user,
  371.                  this.pass, res.objectID);
  372.     return res;
  373. };
  374. /* XMLHttpRequest wrapper code */
  375. /* XMLHttpRequest pool globals */
  376. JSONRpcClient.http_spare = [];
  377. JSONRpcClient.http_max_spare = 8;
  378. JSONRpcClient.poolGetHTTPRequest =
  379. function JSONRpcClient_pool_getHTTPRequest()
  380. {
  381.     if(JSONRpcClient.http_spare.length > 0) {
  382.     return JSONRpcClient.http_spare.pop();
  383.     }
  384.     return JSONRpcClient.getHTTPRequest();
  385. };
  386. JSONRpcClient.poolReturnHTTPRequest =
  387. function JSONRpcClient_poolReturnHTTPRequest(http)
  388. {
  389.     if(JSONRpcClient.http_spare.length >= JSONRpcClient.http_max_spare)
  390.     delete http;
  391.     else
  392.     JSONRpcClient.http_spare.push(http);
  393. };
  394. JSONRpcClient.msxmlNames = [ "MSXML2.XMLHTTP.5.0",
  395.                  "MSXML2.XMLHTTP.4.0",
  396.                  "MSXML2.XMLHTTP.3.0",
  397.                  "MSXML2.XMLHTTP",
  398.                  "Microsoft.XMLHTTP" ];
  399. JSONRpcClient.getHTTPRequest =
  400. function JSONRpcClient_getHTTPRequest()
  401. {
  402.     /* Mozilla XMLHttpRequest */
  403.     try {
  404.     JSONRpcClient.httpObjectName = "XMLHttpRequest";
  405.     return new XMLHttpRequest();
  406.     } catch(e) {}
  407.     /* Microsoft MSXML ActiveX */
  408.     for (var i=0;i < JSONRpcClient.msxmlNames.length; i++) {
  409.     try {
  410.         JSONRpcClient.httpObjectName = JSONRpcClient.msxmlNames[i];
  411.         return new ActiveXObject(JSONRpcClient.msxmlNames[i]);
  412.     } catch (e) {}
  413.     }
  414.     /* None found */
  415.     JSONRpcClient.httpObjectName = null;
  416.     throw new JSONRpcClient.Exception(0, "Can't create XMLHttpRequest object");
  417. };

tip.js

  1. var jsonrpc;
  2. var tipDiv;
  3. var top;
  4. var left;
  5. function onLoad()
  6. {
  7.     jsonrpc = new JSONRpcClient("myjson");
  8. }
  9. function getTip(id , event)
  10. {
  11.     if (navigator.appName == "Microsoft Internet Explorer")
  12.     {
  13.         left = event.x;
  14.         top = event.y;
  15.     }
  16.     else
  17.     {
  18.         left = event.pageX;
  19.         top = event.pageY;
  20.     }
  21.     tipDiv = document.getElementById("tip");
  22.     jsonrpc.tip.getTip(cb, id);
  23.     tipDiv.style.left = left;
  24.     tipDiv.style.top = top;
  25.     tipDiv.style.display = "";
  26. }
  27. function cb(result, exception)
  28. {
  29.     if(result == null && exception != null)
  30.     {
  31.         alert(exception.message);
  32.     }
  33.     if (result != null && exception == null)
  34.     {
  35.         tipDiv.innerHTML=result;
  36.     }
  37. }
  38. function hideTip()
  39. {
  40.     tipDiv.style.display = "none";
  41. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值