ASP.NET mvc angularjs post不重定向

时间:2016-05-08 20:35:27

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

为什么当我向Login RedirectToAction发布帖子请求时,这不起作用且网址不会更改为/Home/Second

AngularJS:

$scope.answer = function(answer) {
  $mdDialog.hide(answer);
  $http({
      method: 'POST',
      url: 'Home/Login',
      data: JSON.stringify({ email: $scope.user.email, password: $scope.user.password })
  });
};

HomeController中:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }
    [HttpGet]
    public ActionResult Second()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(string email,string password)
    {
        return RedirectToAction("Second");
    }
}

1 个答案:

答案 0 :(得分:0)

不是从控制器重定向它,而是可以从Angular js代码重定向它,如下所示;

你的Js代码将是

 $scope.answer = function(answer) {
          $mdDialog.hide(answer);
          var response = $http({
              method: 'POST',
              url: 'Home/Login',
              data: JSON.stringify({ email: $scope.user.email, password: $scope.user.password })
          });

       if(response == "Success")
       {
          $window.location.href = "/Home/Second";
       }

    });

在你的控制器中,你必须像这样编写你的方法:

 public string Login(string email,string password)
 {
        return "Success";
 }