The easiest way is to create a method with the [WebMethod] attribute, and the response will be automatically serialized by JSON. Try it yourself:
[WebMethod] public static string GetDateTime() { return DateTime.Now.ToString(); }
And the url of the Ajax call will look like this:
Page.aspx / GetDateTime
Edit:
To pass parameters, just add them to the function:
[WebMethod] public static int AddNumbers(int n1, int n2) { return n1 + n2; }
I am using jQuery, so the data: object will be set with:
data: "{n1: 1, n2: 2}",
Also note that the returned JSON object will look like this:
{"d":[3]}
The additional "d" in the data is explained here: http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/
John rasch
source share