Founder's blog

Calling ASP.NET Web Services from jQuery

Jul 7 2009 :: by Alex
Another post for ASP.NET developers reading this blog (if any).

I prefer compiling our web-projects to ASP.NET 2.0 rather than 3.5 basically because of all that stuff Visual Studio adds to the "Web.config" file. And the "Web.config" is often edited by our end-users, who might find it confusing to make their way through all these "configSections", "assemblies" and "httpHandlers" that look quite scary.

With the latest project we're working on (a CRM and contact management application) we decided to finally benefit from the MS AJAX framework that is built in to the .NET Framework 3.5.

But to optimize our code and keep it lightweight and fast, we've decided to use jQuery where possible, avoiding the bulky and clumsy MS AJAX's javascript.

So - how do you use jQuery to call a JSON ASP.NET web-service? Here it is. The code is self-explaining:

WebService code:

[WebService(Namespace = "")]
//the next line is important
public class MyWebService : System.Web.Services.WebService
public string HelloWorld(int a, string b)
return "Hello World";

Now the client-side code. Note the JSON-serialized parameters passed with the "data" property:

type: "POST",
url: "MyWebService.asmx/HelloWorld",
data: '{a:1,b:"test"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {

'Calling ASP.NET Web Services from jQuery' was written by Alex by Alex. CEO, founder