[英]401:unauthorized when I try to call a method via AJAX in .net
I have a .net project in the framework 4.5.1我在框架 4.5.1 中有一个 .net 项目
When I use ajax
like below, it works and the debugger hits the page_load
event:当我像下面这样使用ajax
,它可以工作并且调试器会触发page_load
事件:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx",
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
In my advanced.aspx.cs , I have the method below:在我的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;
}
I want to hit this method via ajax
.我想通过ajax
这个方法。 To do this, I am updating my ajax
call like below:为此,我正在更新我的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');
}
})
However, it gives me 401:unauthorized error
when i do it in that way, shown below:但是,当我这样做时,它给了我401:unauthorized error
,如下所示:
How can I fix this?我怎样才能解决这个问题? Tried lots of things for the last few hours, no luck.. Any help would be appreciated.在过去的几个小时里尝试了很多东西,没有运气.. 任何帮助将不胜感激。
EDIT: I've used async
and crossDomain
hoping to fix this issue, didn't help.编辑:我使用async
和crossDomain
希望解决这个问题,没有帮助。 Normally they don't exist in my ajax call, nothing changes.通常它们在我的 ajax 调用中不存在,没有任何变化。
Try updating your AJAX JS to:尝试将您的 AJAX JS 更新为:
$.ajax({
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {myval:"test"},
success: function (ms) {
alert('success');
}
})
Found the answer here: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"在这里找到答案: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
I have used settings.AutoRedirectMode = RedirectMode.Off;
我用过settings.AutoRedirectMode = RedirectMode.Off;
instead of RedirectMode.Permanent
in ~/App_Start/RouteConfig.cs
, which fixed the issue.而不是RedirectMode.Permanent
在~/App_Start/RouteConfig.cs
,其中固定的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.