gpt4 book ai didi

c# - ASP.NET 使用 jQuery AJAX 调用 WebMethod "401 (Unauthorized)"

转载 作者:IT王子 更新时间:2023-10-29 03:56:37 24 4
gpt4 key购买 nike

坚持了好几个小时

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

我试图在我的 ASP.Net Webform 中调用这个 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);
}
});
}

但我总是得到这个错误:

POST 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>

有什么想法吗?

最佳答案

问题解决了

这让我发疯。

~/App_Start/RouteConfig.cs 内更改:

settings.AutoRedirectMode = RedirectMode.Permanent;

收件人:

settings.AutoRedirectMode = RedirectMode.Off;

(或者只是注释该行)

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

url: "ConsultaPedidos.aspx/GetClients",

收件人:

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

希望这对其他人有帮助

关于c# - ASP.NET 使用 jQuery AJAX 调用 WebMethod "401 (Unauthorized)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com