JSON Messaging
From RemObjects Wiki
This is a HowTo topic
about RemObjects SDK for .NET
Feel free to add your notes to this topic below.
With the latest RemObjects SDK we've introduced JSON-RPC support. JSON-RPC is a way of doing remote calls on top of JSON. JSON is a simple, human readable data interchange format written in subset of Javascript syntax. JSON-RPC is ideal for calls from a Javascript client, and the Service Builder includes a header generator for Javascript that uses a JSON library from the Yahoo User Interface library. Of course RO isn't limited to YUI, and can easily be used with other JSON implementations.
To use JSON, just insert a new JSONMessage and add it to your servers' dispatchers list. To use from Javascript, open the Service Builder for your project and use the CodeGen/Javascript/JSON-Rpc Interface (*.js) menu item.
For a simple service with a Sum(a,b) method the code generator will generate code like:
// Javascript JSON-RPC Code Generated for the NewLibrary library. /* This codegen depends on the Yahoo YUI (http://developer.yahoo.com/yui/) toolkit and requires the following libraries. Note that it's recommended to download yui and place a local copy next to your html. <script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo/yahoo.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/json/json.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/connection/connection.js"></script> */ // Service: MyService function MyService(url){ this.url = url; } MyService.prototype.Sum = function(A, B, __callback) { var __message = { "method" : "MyService.Sum", "params" : { "A": A, "B": B } } var __callbacks = null; if (__callback) { __callbacks = { callback : __callback, success : function (o) { var __result = YAHOO.lang.JSON.parse(o.responseText); if (__result.error) { if ((typeof this.callback == "object") && this.callback.failure) this.callback.failure(__result.error); } else { if ((typeof this.callback == "object") && this.callback.failure) this.callback.success(__result.result); else this.callback(__result.result); } }, failure : function (o) { if ((typeof this.callback == "object") && this.callback.failure) this.callback.failure(o); }, timeout : 10000 } } YAHOO.util.Connect.asyncRequest("POST", this.url, __callbacks, YAHOO.util.Lang.JSON.stringify(__message)); } // End of service: MyService
You can put this in an external js file or in your html file. To use this, create the MyService object and call Sum. Due to the nature of Javascript, the last parameter of every service function is a callback or an object with success and failure callbacks. For example:
var service = new MyService("http://hostname/path/service.ashx/JSON"); service.Sum(1, 5, function(res) { alert('Result: '+res); } ); // or: service.Sum(1, 5, { success: function(res) { alert('Result: '+res); } failure: function(error) { alert('Error while processing sum request'); } );
Make sure the target url passed to MyService is on the same server as the calling html, as a browser won't allow calls to foreign servers. The result error in the failure is the value of the error passed from json, or an exception from the http handler.
See Also
- Lists
- Namespace Lists for .NET — Java
- Interface Lists for .NET — Delphi — Java
- Protocol List for Xcode
- Class Lists for .NET — Xcode — Delphi — Java
- Object List for JavaScript
- Enum Lists for .NET — Xcode — Delphi — Java
- Alias Lists for Xcode — Delphi
- Implements Lists for .NET — Xcode — Delphi — Java
- Data Types
- File Types
Product: RemObjects SDK
Available Editions: RemObjects SDK for .NET, Delphi, Xcode, Java and JavaScript
Glossary — Architecture — Articles — Features — Library — Samples