已经被困了好几个小时了

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

我正在try 在我的ASP中调用此WebMethod.网络表单

[WebMethod]
public static string GetClients(string searchTerm, int pageIndex)
{
    string query = "[GetClients_Pager]";
    SqlCommand cmd = new SqlCommand(query);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
    cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
    cmd.Parameters.AddWithValue("@PageSize", PageSize);
    cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
    return GetData(cmd, pageIndex).GetXml();
}

从这个jquery.AJAX

function GetClients(pageIndex) {
    $.ajax({
        type: "POST",
        url: "ConsultaPedidos.aspx/GetClients",
        data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
    });
}

但我总是会犯这样的错误:

邮政http://localhost:64365/ConsultaPedidos.aspx/GetClients 401 (Unauthorized)

奇怪的是,在我开始对用户进行身份验证之前,这种方法一直有效

<system.web>
...
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="/Dashboard" />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
...
</system.web>

有什么 idea 吗?

推荐答案

问题解决了

这让我发疯.

~/App_Start/RouteConfig.cs内换:

settings.AutoRedirectMode = RedirectMode.Permanent;

致:

settings.AutoRedirectMode = RedirectMode.Off;

(或者只是 comments 一下这句话)

此外,如果启用了友好URL,则需要更改

url: "ConsultaPedidos.aspx/GetClients",

致:

url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',

希望这能帮助别人

Asp.net相关问答推荐

缓存httpmessage内容

在哪里可以记录 ASP.NET Core 应用程序的启动/停止/错误事件?

IIS 中 ASP.net 应用程序的单独应用程序池

在 appSettings 中存储字符串数组?

ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile("appsettings.json");找不到文件

Window.Open 使用 PDF 流而不是 PDF 位置

如何在asp.net中判断会话是否过期

Gridview ItemTemplate 中多个判断字段的最佳技术?

Devexpress 或 Telerik Controls 比较

ASP.NET MVC 5 中的路由可选参数

如何将 css 类添加到 ASP.Net 中的更新面板?

在 ASP.NET MVC 中使用 Razor 创建可重用的 HTML 视图组件

单击按钮时播放哔声

如何在 ASP.NET 中设置下拉列表项?

Cache.Add 绝对过期 - 是否基于 UTC?

如何通过 Google 图表 API 使用 X 轴中的日期?

GridView 按代码隐藏列

ASP.NET Web 窗体中的 jQuery 验证插件

在 ASP.NET 中使用 SecureString 有什么好处吗?

jQuery Ajax 调用 - 成功设置变量值