jquery ajax 发布成功后,RedirectToAction 不起作用? [英] RedirectToAction not working after successful jquery ajax post?

查看:41
本文介绍了jquery ajax 发布成功后,RedirectToAction 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容不会重定向我的页面:这是 MVC 代码:

The following does not redirect my page: Here is the MVC code:

    [HttpPost]
    public ActionResult GoHome()
    { 
         return RedirectToAction("Index", "Home");   
    }

这是ajax帖子:

   $.support.cors = true;

            $.ajax({
                type: "POST",
                url: "http://localhost/UserAccount/GoHome",
                dataType: 'json',
                crossDomain: true
            });

发布成功,当它执行 GoHome 动作时,它不会重定向到 Home 控制器的索引动作.

The post is successful and when it hists the GoHome action it does not redirect to the Index Action of the Home Controller.

推荐答案

您不能从 AJAX 帖子重定向.但是,您可以返回要将浏览器重定向到的 URL 并从 Javascript 重定向.

You cannot redirect from an AJAX post. You could return the URL you want to redirect the browser to however and redirect from Javascript.

控制器

[HttpPost]
public ActionResult GoHome()
{ 
     return Json(Url.Action("Index", "Home"));   
}

Javascript

$.ajax({
    type: "POST",
    url: "http://localhost/UserAccount/GoHome",
    dataType: 'json',
    crossDomain: true,
    success: function (data) {
        window.location.href = data;
    }
});

这篇关于jquery ajax 发布成功后,RedirectToAction 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆