Authentication failed when calling a web method using $ .ajax

When I make a jQuery call, I get an authentication error message:

{ 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()); } }); }); }); 

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 AutoRedirectMode to Off in RouteConfig.cs . This stops the error, but the web method has not been called. Any suggestions?

+6
source share
1 answer

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

 settings.AutoRedirectMode = RedirectMode.Off; 

and adding a ScriptManager to an aspx page with the EnablePageMethods parameter set to true:

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True"> </asp:ScriptManager> 
+4
source

Source: https://habr.com/ru/post/986100/


All Articles