ASP.NET WebMethod Call with jQuery AJAX Returns "401 (Unauthorized)"
This issue occurs when calling a WebMethod in ASP.NET with jQuery AJAX after implementing user authentication in the application. The user is authenticated, but the WebMethod call fails with a 401 (Unauthorized) error.
To resolve this issue, check the following solutions:
Modify RouteConfig.cs: In the ~/App_Start/RouteConfig.cs file, disable the automatic URL redirection by commenting or changing the following line:
settings.AutoRedirectMode = RedirectMode.Permanent;
To:
settings.AutoRedirectMode = RedirectMode.Off;
Specify Absolute URL: If friendly URLs are enabled, replace the URL in the AJAX call with the absolute path to the WebMethod. For example, change:
url: "ConsultaPedidos.aspx/GetClients",
To:
url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',
By implementing these fixes, the WebMethod call should now succeed with authentication properly enforced.
The above is the detailed content of Why Does My ASP.NET WebMethod Return a 401 (Unauthorized) Error After Authentication?. For more information, please follow other related articles on the PHP Chinese website!