jquery - .net 和 jquery 突然给我 401 错误

标签 jquery asp.net asp.net-ajax

这是我的 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/

相关文章:

javascript - 使用 JavaScript 将标记添加到 Highstock 图表 (highcharts)

c# - Struct 不缓存,但 Class 缓存

html - ItemTemplate Repeater ASP.NET 内部的 Div 大小

javascript - 可滚动 GridView 不能与 ASP.Net 中的 UpdatePanel 一起使用

jQuery切换类不改变CSS

javascript - jQuery 更改事件代码未运行可能的事件冲突

javascript - 为什么选中复选框时 DOM 没有更新?

C# 模型图

web-services - ASP.NET Web 服务错误地返回 XML 而不是 JSON

javascript - 本地化 <asp :FileUpload>. IE 问题