Client Class ServiceProxy
The ServiceProxy class lets you call Microsoft ASP.NET AJAX based WCF or ASMX JSON services. It handles JSON serialization and deserialization as well as URL and parameter fixups for data passed.

The key feature of this class is the invoke method which is used to invoke a server side method and call the specified callback handler with the result.

Server method Interface signature (WCF):

[OperationContract]          
[WebInvoke(Method="POST",
   BodyStyle=WebMessageBodyStyle.Wrapped,
   ResponseFormat=WebMessageFormat.Json)]
StockQuote GetStockQuote(string symbol);

JavaScript client code to call it.

// Create a static instance (global so it can be reused)
var serviceUrl = "JsonStockService.svc/"; // specify service 'base' url
var proxy = new serviceProxy(serviceUrl);

function GetStockQuote()
{       
    var symbol = $("#txtSymbol").val();            
    if (!symbol)
    {
        showStatus("Please provide a symbol value.",5000);
        return;
    }    
    
    // pass single symbol parameter
    proxy.invoke("GetStockQuote",{"symbol":symbol},
        function (result)  // returns a quote object
        {   
            if (!result)
            {
                showStatus("Invalid Stock Symbol");
                return;
            }
            showStockProgress(true);
            
            
            $("#StockName").text( result.Company + " (" + result.Symbol + ")" ) ;
            $("#LastPrice").text(result.LastPrice.toFixed(2));
            // additional ui assignments
        },
        function(error) { alert(error.message); },
        false); 
}

Class Members

MemberDescription
invoke Calls a service method by specifying a method name, an array of parameters and a callback and error handler method.
o.JsonService.invoke(method, params, callback, errorCallback, isBare)
isWcf If true the request is set up for calling a WCF service and performs special date encoding when creating JSON. Required only for WCF services, not for ASMX services.
serviceUrl The url to the WCF or ASMX JSON service to be called. Can also be set via the constructor. This value needs to be set prior to calling invoke().
timeout The timeout for the the xhr request. Default is 20,000 milliseconds.


© West Wind Technologies, 1996-2015 • Updated: 02/09/2012
Comment or report problem with topic