javascript - 重定向到本地不起作用

标签 javascript .net angularjs asp.net-mvc asp.net-mvc-4

我有一个登录页面,该页面也通过使用 Angular 的 View 进行调用。

一旦它点击“RedirecToLocal”命令..我单步执行,我看到它点击了我的主 Controller 并返回索引 View 就好了..但是当涉及到我的浏览器实际将页面更改为索引时,它就赢了没有改变并且在登录屏幕上保持糟糕。我不明白为什么。我要疯了

账户 Controller

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
    [HttpPost]
    [AllowAnonymous]
    public ActionResult Login(LoginFormModel form, string selectedShow)
    {
        if (ModelState.IsValid && WebSecurity.Login(form.userName, form.password))
        {
            ...code here...
        }
        return RedirectToLocal("/");  //HITS HERE SUCCESSFULLY

家庭 Controller

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();  //HITS HERE SUCCESSFULLY
    }
}

编辑:

HTML 和 Angular

    <button id="loginBtn" ng-click="login()" ng-class="{ 'disabled': isLoading }" class="btn btn-large btn-primary btn-block">
  <span>{{buttonText}}</span>
  </button>

Angular 登录 Controller :

AccountFactory.login($rootScope.formData);

帐户工厂

AccountFactory.login = function (formData) {
    return $http({
        method: 'POST',
        url: '/Account/Login',
        data: formData
    });
};

最佳答案

当通过 $http 使用 Ajax 时,出于安全考虑,浏览器不会接受服务器发出的重定向。

使用 ajax 时,处理指示授权的成功返回并通过客户端路由(您喜欢的风格)进行重定向。

AccountFactory.login($rootScope.formData).then(function (data) {
    $location.path("/");
});

由于 .then() 被视为成功回调,因此可接受的模式是从 API 返回成功的 200 状态代码以表示成功,并返回 40x 授权尝试失败。

[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginFormModel form, string selectedShow)
{
    if (ModelState.IsValid && WebSecurity.Login(form.userName, form.password))
    {
        ...code here...
        return new HttpStatusCodeResult(200);
    }
    else 
    {
        return new HttpStatusCodeResult(403);
    } 
}

关于javascript - 重定向到本地不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961316/

相关文章:

javascript - 在 AngularJS 中突出显示当前事件菜单的正确方法是什么?

Javascript 在多个定界符处拆分,同时保留定界符

c# - 静态异步类的模拟单元测试和依赖注入(inject)

asp.net - 在我的 VS 2013 环境中获取 ASP.NET 4.5(安装或升级)

asp.net - 更改 IIS 域 URL

javascript - Angular 插入数组给出 : TypeError: Cannot read property 'push' of undefined

javascript - 如何增加图像的大小以使其可见?

javascript - 拖放功能在触摸设备中不起作用?

javascript - 使用 Node.js 函数中的数据

javascript - 底部图片未齐平位于页面底部