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 is always JSON


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");


Returning Arbitrary Data using Streams

You can also return arbitrary data from a [CallbackMethod] call by returning a Stream object. When a Stream is returned CallbackHandler returns the result as a raw HTTP stream. This can be useful to allow your AJAX 'service' also return content such as images or even HTML responses that might be needed in addition to common data responses served as JSON.

To implement you can do something like the following:

[CallbackMethod] public Stream GetStockHistoryGraphSafe(string symbolList, string title, int width, int height, int years) { string[] symbols = symbolList.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries); return this.GetStockHistoryGraph(symbols, title, width, height, years); if (years <1) years = 2; HttpContext.Current.Response.ContentType = "image/png"; byte[] imgData = Stocks.GetStockHistoryGraph(symbols, title, width, height, years); MemoryStream ms = new MemoryStream(imgData); return ms; }

This method returns an image as a binary stream which is returned as raw data by CallbackHandler. Note that you have to specify the content type of the data returned in the method so that IIS can serve the image properly as an image. Note that HttpContext.Current is available and allows you to adjust any headers for the response as needed.

To access the GetStockHistoryGraphSafe method via a GET Url you can use (all on one line):

JsonStockService.ashx?Method=GetStockHistoryGraphSafe
&symbolList=Msft,INTC,KMP
&Title=Title
&Width=400
&Height=250
&Years=2

which produces a stock chart image. You can use this in an AJAX call that on return sets an image control with this url to display the new graph.

This isn't an AJAX feature per se as this data is just a data call to retrieve non-JSON data - it's just a way to encapsulate related functionality into an AJAX service so only the service feeds 'data' to the client.