Note: This post is part of a long-running series of posts covering the union of jQuery and ASP.NET: jQuery for the ASP.NET Developer.

Topics in this series range all the way from using jQuery to enhance UpdatePanels to using jQuery up to completely manage rendering and interaction in the browser with ASP.NET only acting as a backend API. If the post you're viewing now is something that interests you, be sure to check out the rest of the posts in this series.

Mea culpa: I may owe your fingers an apology.

If you’ve been using my approach for directly calling ASP.NET’s JSON-based services with jQuery, you know that specifying the correct Content-Type on your requests to them is a crucial part of coaxing JSON out of them. However, when I suggested that a Content-Type of application/json; charset=utf-8 was necessary, that was overly specific.


As it turns out, only the application/json portion is necessary.

More accurately, ASP.NET checks that your request’s Content-Type header begins with application/json. Beyond that, you can even specify a charset of acrobatic-unicorns and it still works (no, really; I tried).

That may only sound like a minor reduction in typing, but I find that it makes a non-trivial improvement in how memorable the parameter is. At least for me, the punctuation in ; charset=utf-8 made that Content-Type string tedious to remember and I would often resort to copy/paste to be sure that it was correct.

It’s nice to be able to quickly type out an $.ajax() call to your services without tedious memorization, online reference, or copy/paste.

Bonus: Ditch the dataType

When I originally wrote about using jQuery with ASP.NET’s web services, the current jQuery version was 1.2 and jQuery’s $.ajax() method looked nothing like what it does today. One of the changes that occurred over the years is that jQuery 1.4 added support for parsing responses based on their Content-Type.

Before that addition, it was imperative that you specify a dataType of json if you wanted jQuery to automatically handle converting JSON responses to JavaScript objects. Beginning with version 1.4, responses with the Content-Type application/json are automatically treated as JSON and parsed accordingly.

Since ASP.NET does set the correct Content-Type on its JSON-serialized responses, manually specifying a dataType is no longer necessary as long as you’re using jQuery 1.4 or later.

So, what’s left?

Eliminating all that dead weight, your AJAX calls to ASMX ScriptServices, Page Methods, and WCF services can now be simplified to this:

Sometimes it’s the little things…