[英]401 Unauthorized error when making ajax call to asp.net web api endpoint with window authentication enabled
[英]401:unauthorized when I try to call a method via AJAX in .net
我在框架 4.5.1 中有一個 .net 項目
當我像下面這樣使用ajax
,它可以工作並且調試器會觸發page_load
事件:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx",
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
在我的advanced.aspx.cs 中,我有以下方法:
[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string SaveChange(string myval)
{
//dummy code for test purposes.
string u = myval + "X";
return u;
}
我想通過ajax
這個方法。 為此,我正在更新我的ajax
調用,如下所示:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {username:"test"},
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
但是,當我這樣做時,它給了我401:unauthorized error
,如下所示:
我怎樣才能解決這個問題? 在過去的幾個小時里嘗試了很多東西,沒有運氣.. 任何幫助將不勝感激。
編輯:我使用async
和crossDomain
希望解決這個問題,沒有幫助。 通常它們在我的 ajax 調用中不存在,沒有任何變化。
嘗試將您的 AJAX JS 更新為:
$.ajax({
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {myval:"test"},
success: function (ms) {
alert('success');
}
})
在這里找到答案: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
我用過settings.AutoRedirectMode = RedirectMode.Off;
而不是RedirectMode.Permanent
在~/App_Start/RouteConfig.cs
,其中固定的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.