Using CallbackHandler with GET Requests

You can also access CallbackHandler HttpHandlers with Get syntax. The syntax for a service call with Get is:

Handler.ashx?Method=MethodToCall&symbol=MSFT&years=Title

You need to specify the method name and query string variables that match the name of each of the parameters including case. Values are treated as string inputs that are converted to the their proper types in the current locale. Use null for null values.

To call a server method set up like this:

[CallbackMethod]
public StockQuote GetStockQuote(string symbol)
{
	return Stocks.GetStockQuote(symbol);
}

you'd use:

StockService.ashx?Method=GetStockQuote&symbol=MSFT

which produces a JSON response for the Stock Quote object. You can use plain jQuery to retrieve content for this:

$.getJSON("ajax/StockPortfolio/JsonStockService.ashx?Method=GetStockQuote&symbol=MSFT",
       null,                 
       function(json) {
           alert(json.LastPrice);
       });

Note that $.getJSON does not handle date decoding. You can use ww.jquery's ajaxJSON:

ajaxJson("ajax/StockPortfolio/JsonStockService.ashx?Method=GetStockQuote&symbol=MSFT",
            null,
            function(stockQuote) {
                alert(stockQuote.LastPrice + " " + stockQuote.LastQuoteTime);
            }, onPageError, 
            { method: "GET" });

which has the added advantage that it has error handling support should something go wrong during the request.

**A few Things to watch out for with GET** * Only simple .NET types can be passed as parameters (string,bool,numbers,dates) * All parameters must be passed via GET query string * Query string keys must match parameter names *Specify param=null for null parameters * The response output (JSON or XML) is determined by the Accept header

Direct Access to WebPage [CallbackMethod] methods

You can also return access page methods directly by using an additional query string paramater:

CustomerList.aspx?CallBackTarget=Proxy&Method=GetCustomerList

CallbackTarget is the name of the Callback control on the page that is responsible for the call that is fired.

These URLs can be used directly from JavaScript to components that directly load JSON data into their own properties. For example, the jqGrid plug-in asks for a data url which can be served with this setup:

$("#gdCustomers").jqGrid({
    **url: "jqGrid.aspx?CallbackTarget=Proxy&Method=GetCustomers",**  
    colNames: ["Name", "Company", "Entered", "Billing Rate"],
    colModel: [
      { name: "Name", index: "ContactName", width: 100 },
      { name: "Company", index: "CompanyName", width: 100 },
      { name: "Entered", index: "Entered", width: 75 },
      { name: "BillingRate", index: "BillingRate", width: 50 }
      ],
    pager: $("#gdCustomersPager"),
    sortname: "Company",
    sortorder: "asc",
    caption: "Customer List"
})
.navGrid("#gdCustomersPager");


© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic