Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Wednesday, 10 November 2010

Getting 401 Unauthorized error consistently with jquery call to webmethod

ASP.NET with JQuery provides a flexible method of making ajax calls via a webservice. Making an ajax call using JQuery API would be as simple as

            url: "TestService.asmx/Test",
            data: "{'JSONData':'" + JSON.stringify(id) + "'}", // For empty input data use "{}",
            dataType: "json",
            type: "POST",
            contentType: "application/json",
            complete: function (jsondata, stat) {
                if (stat == "success") {
                    var response = JSON.parse(jsondata.responseText).d;
            error: function (xhr, ajaxOptions, thrownError) {


Now the webservice method definition should look like:-

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Test(string JSONData)
   return JSONData + " Test Data";

Now that we have the code ready, lets try to run this. Oh No!! ASP.NET bounced back with following error.

"401 Unauthorized"

This internally means JQuery Ajax call had responded as System.InvalidOperationException and hence that would be thrown as 401 Unauthorized exception.

The simple solution is to include the following line before the class definition of the webservice


“Real knowledge is to know the extent of one’s ignorance.”
– Confucius


  1. I have this error, but when calling a webmethod in the code behind of the page. How would I go about implementing this solution without resorting to creating a webservice?

    I've got many examples of this working perfectly well with the webmethod in the code behind, it's just on this one instance i get a 401. Very annoying.

  2. Did you mean page methods ? as web methods are available only via web service.

  3. I have been using ASP.NET Ajax webservices for years without any problems.

    Since trying to implement $.ajax(), I consistently get the "401 Unauthorized" error in my dev workstation. Cannot get the jQuery.ajax syntax to work. It does work on my home workstation, but not on my Office machine. They have the same webservice files.

  4. Have you used fiddler to see where the request is going to ? check your url: ~/TestService.asmx/Test"


Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers