这是我的 ajax 调用:
$J.ajax({
type: 'POST',
url: "/ajax/getCharts",
data: JSON.stringify(cids),
dataType: 'json',
asynch: false
})
这一直有效……直到昨天
我们正在使用 Microsoft.AspNet.Identity;
昨天我添加了更多用户角色,扩展了默认设置,例如“admin”、“user”等。
现在我在我的 ajax.fail header 中得到了这个错误:
X-Responded-JSON: {"status":401,"headers":{"location":"http://localhost:56970/Account/Login?ReturnUrl=%2Fajax%2FgetCharts"}}
哪种方式有意义 - 如果调用未通过身份验证
注意:
在我的 ajaxController 中,我之前曾这样注释它(有效):
[Authorize(Roles = "User")]
public class ajaxController : BaseController
现在我扩展了它,包括我的新自定义用户角色,这样:
[Authorize(Roles = "Superuser, Poweruser, User")]
public class ajaxController : BaseController
那没用
我还在 RegisterRoutes 中添加了这个:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
建议:
ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
但是没用
这个页面对我来说毫无意义:
jquery $.ajax call results in 401 unauthorized response when in Chrome or Firefox, but works in IE
我在这里错过了什么?
最佳答案
我能理解的问题是当用户尝试登录时说我刚刚发送了登录请求..此时我没有登录意味着我目前没有任何角色并且你已经将授权过滤器放入ajax Controller 允许一些角色
现在要解决此问题,请将 allowannonymus 属性仅添加到 ajax Controller 中的登录操作
喜欢
[AllowAnonymous]
public async Task<ActionResult> Login()
{
}
p.s :- 不要忘记把它放在 post action 和 register action 中,如果你有的话
关于jquery - .net 和 jquery 突然给我 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45475885/