Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "401 Unauthorized" error consistently with jquery call to webmethod

I have been struggling to get my jquery call to a webmethod to work. I am being bounced by the server with a "401 Unauthorized" response. I must have an incorrect setting in the web.config or somewhere else that would be preventing a successful call.

Your insight is appreciated!

Call to js function the invokes the jquery call

button.OnClickAction = "PageMethod('TestWithParams', ['a', 'value', 'b', 2], 'AjaxSucceeded', 'AjaxFailed'); return false;";

JavaScript function that makes the jquery call

function PageMethod(fn, paramArray, successFn, errorFn) {
var pagePath = window.location.pathname;
var urlPath = pagePath + "/" + fn;

//Create list of parameters in the form:  
//{"paramName1":"paramValue1","paramName2":"paramValue2"}  
var paramList = '';
if (paramArray.length > 0) {
    for (var i = 0; i < paramArray.length; i += 2) {
        if (paramList.length > 0) paramList += ',';
        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
    }
}
paramList = '{' + paramList + '}';

//Call the page method
$.ajax({
    type: "POST",
    url: pagePath + "/" + fn,
    contentType: "application/json; charset=utf-8",
    data: paramList,
    timeout: 10000,
    dataType: "json",
    success: function(result) { alert('Overjoyed'); },
    error: function(result) { alert('No joy'); }
});
}

Web method in page

    public partial class WebLayout : System.Web.UI.Page
{

    [WebMethod()]
    public static int TestNoParams()
    {
        return 1;
    }

    [WebMethod()]
    public static string TestWithParams(string a, int b)
    {
        return a + b.ToString();
    }
...

Response as seen in Firebug console

json: {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

and

"NetworkError: 401 Unauthorized - http://localhost/Care-Provider-Home/Profile/Personal-Profile.aspx/TestWithParams" TestWithParams

I have looked at and read the usual sites on the subject (Encosia, et al), but to avail. Either I am missing a critical piece, or there are some subtleties in the security parameters of my environment that preventing a call.

Here are some other potentially useful tidbits that may impact your diagnosis:

  • Webmethods in codebehind
  • Using Sitecore CMS (Does not seem to intefere, never know)
  • IIS7
  • .NET 3.5
  • jQuery 1.3.2

I look forward to your insights and direction--thank you!

like image 365
CareMatch Avatar asked Feb 19 '10 16:02

CareMatch


2 Answers

Yes, it did get working! Since Sitecore CMS does perform URL rewriting to generate friendly URLs (it assembles the pages in layers, dynamically, similar to Master Page concept), it occurred to me that it may be causing some problem the initially caused the 401 error. I verified this by creating a separate project with a single ASPX--and with some work I was able call the web methods and get values using the jquery. I then created nearly identical ASPX in my web root, but told Sitecore to ignore it when a request is made to it (IgnoreUrlPrefixes in the web.config), after some work I was able also get it to work successfully! Thanks for your help.

like image 136
CareMatch Avatar answered Oct 14 '22 19:10

CareMatch


The json response from the Firebug Console provides the most telling clue IMO. The System.InvalidOperationException (which strangely rides on a 401 response) suggests something more is at work.

First, googling on "InvalidOperationException webmethod jquery" returns articles which suggest serialization problems can throw this exception. To rule this out, temporarily change "data: paramList" to "data: '{}'". In addition, attach a debugger and see if the exception happens before the method executes or after it completes and attempts to serialize the result.

If the steps above come up empty, you may want to try resetting to a clean web.config or read more of the results that come back from the "InvalidOperationException webmethod" search

like image 36
John Lewin Avatar answered Oct 14 '22 19:10

John Lewin