使用$ .ajax调用Web方法时,身份验证失败错误

Aaz*_*Aaz 6 c# asp.net ajax jquery webmethod

当我进行JQuery调用时,我收到一个身份验证失败的响应:

{
    Message: "Authentication failed.", 
    StackTrace: null, 
    ExceptionType: "System.InvalidOperationException"
}
Run Code Online (Sandbox Code Playgroud)

jQuery调用:

$(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());
            }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

网络方法:

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

我在web.config中添加了以下内容:

<authorization>
    <allow users="*" />
</authorization>
<authentication mode="None" />
Run Code Online (Sandbox Code Playgroud)

我尝试设置AutoRedirectModeOffRouteConfig.cs.这样可以阻止错误,但仍然无法调用Web方法.有什么建议?

Aaz*_*Aaz 4

通过在 App_Start/RouteConfig.cs 中将 AutoDirectMode 设置为 Off 来解决

settings.AutoRedirectMode = RedirectMode.Off;
Run Code Online (Sandbox Code Playgroud)

并将 ScriptManager 添加到将 EnablePageMethods 设置为“true”的 aspx 页面:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
    </asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)