Authentication failed error when calling web method using $.ajax
Asked Answered
N

1

6

When I make a JQuery call, I get an Authentication failed response:

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

jQuery call:

$(document).ready(function () {
    //Handle the change event for the drop down list
    $("#ddRegions").change(function () {
        //create the ajax request
        $.ajax({
            type: "POST", //HTTP method
            url: '<%= ResolveUrl("WebForm2.aspx/getLocations")%>', //page/method name
            data: "{}", //json to represent argument
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) { //handle the callback to handle response                
                //request was successful. so Retrieve the values in the response.
                alert(msg.toSource());
            }
        });
    });
});

The web method:

public static string getLocations() {
    System.Diagnostics.Debug.WriteLine("Getting Locations.");
    return "{region:auckland, city:auckland}";
}

I added the following to my web.config:

<authorization>
    <allow users="*" />
</authorization>
<authentication mode="None" />

And I tried setting the AutoRedirectMode to Off in RouteConfig.cs. This stops the error, but still the web method never gets called. Any suggestions?

Needlefish answered 28/4, 2015 at 6:21 Comment(9)
Did you try using $.support.cors = true; in jquery to enable cross origin this is restricted from browsers for security purpose?Sharpen
When you say it never gets called, does the browser send the request? If so can you please show the response and request using Fiddler or Google Chrome Developer Tools?Lancer
Can this help you? forums.asp.net/t/…Mesitylene
Does this even postback firstly ?Andorra
dataType: 'json' forces jquery to parse the response data automatically. But the getLocations() method returns invalid json. It should be: {"region":"auckland", "city":"auckland"}. The double quotes are very important for JSON.parse().Gravity
You sure your call is not returning? (I don't see a definition for toSource(). Try changing your success callback to alert(msg.d)). Also how about the [WebMethod] decorator on to of your server side function. If all else fails, try disabling the RegisterRoutes as well for a while.Cocktail
Thank for the suggestions. $.support.cors did not work. I changed the response string but it still doesn't get that far. alert(msg.d) returns undefined. Sorry if this is a stupid question, but how do I check the postback in this instance?Needlefish
However.... Using AutoRedirectMode.Off I get the message: "Unknown web method getLocations." in the response.Needlefish
Just for information : codeproject.com/Articles/655086/…Contemn
N
4

Resolved by setting AutoDirectMode to Off in App_Start/RouteConfig.cs

settings.AutoRedirectMode = RedirectMode.Off;

and adding a ScriptManager to the aspx page that has an EnablePageMethods set to 'true':

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
    </asp:ScriptManager>
Needlefish answered 28/4, 2015 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.