Import the client-side proxy by using the JsonGateway.aspx URL as the script source and using ?proxy as the query string. The destination and source parameters are used to select the remoting service. Source is required only when the remoting destination has asterisk(*) as source.
This proxy code will contain a class that you can instantiate and use to call the remoting service either synchronously and asynchronously.
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="jsongateway.aspx?proxy&destination=fluorine&source=ServiceLibrary.MyService"></script>
<script type="text/javascript">
/* <![CDATA[ */
window.onload = function()
{
var s = new MyService();
alert("sync:" + s.Echo("some text"));
s.Echo("some text", function(response) { alert("async:" + response.result) });
}
/* ]]> */
</script>
When the last parameter supplied is a JavaScript function then it is treated as a callback. The call is made to the service and control returned immediately. When the response becomes available, the proxy invokes the callback function and sends the JSON-RPC response object as parameter. The result property of this object can then be consulted to obtain the return value from the server-side RPC method.
On failure, the response object instead contains an error property.
The error object contains message and code properties.
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="jsongateway.aspx?proxy&destination=fluorine&source=ServiceLibrary.MyService"></script>
<script type="text/javascript">
/* <![CDATA[ */
window.onload = function()
{
var s = new MyService();
try
{
s.Echo("some text"));
}
catch( error )
{
alert( error.message );
alert( error.code );
}
}
/* ]]> */
</script>