Tuesday, July 7, 2009

Client side consummation of a web service: Is ASP.NET AJAX or jQuery better to use?

A couple of weeks ago, as I was working on a Google Maps application, I had to consume a web service from the client script. Not being very accustomed to client scripting, I did quite a lot of looking around before I figured out which solution would be most secure and have the least possible payload.

My top two possibilities were AJAX and jQuery, but before I jump to my conclusion let’s take a look at some code!

The web service:

Create a regular asmx web service. Note that the service must have a ScriptService attribute which allows the web service to be called from the script. To make the example a bit more interesting I’ve given the ExampleWebMethod a parameter.

Using AJAX:

The first thing to remember when using AJAX to call the web service is to include a ScriptManager control with a ServiceReference tag in your aspx/ascx:

Calling the web service in the client script is now very easy:

Using jQuery:

jQuery has an .ajax() command which is more secure than the jQuery’s getJSON() command. As the request done is a HTTP POST request and the contentType has to be "application/json; charset=utf-8”, it will to a further extent protect your web service from being attacked than many other solutions would do.


Although the AJAX way of calling a web service looks easier there are many reasons why jQuery is a better solution. When using AJAX, the framework somewhat takes care of the security for you, but one can never be too secure and therefore jQuery has its advantage at this point.

Another important question is the performance of the two scripts. Using jQuery rather than AJAX ensures fewer HTTP requests and apparently less payload. So the winner is clear: In my opinion, jQuery seems like the best and safest solution!

And for those of you who are wondering about the Google Maps application: More on that later!


  1. Interesting stuff! I would be a bit more correct about the term 'ajax' tho, and say 'asp.net' or something when referring to that alternative, but that's just nitpicking. Looking forward to your thoughts on Google Maps.

  2. I actually did that at first, but then I assumed it would be enough to say mention that it was asp.net ajax in the header. But you're right, you can never be too clear ;)

  3. From what I've read, jQuery also provides less overhead than .NET's solution for AJAX.

  4. Skriv noe jeg forstår da Karoline ;)

    Morsomst er jo at Thomas-hyttenabo har kommentert på dette! Hei Thomas!

    Geeks... ;)