如何在控制器的操作方法中重定向Ajax发布? [英] How do I do a redirect an Ajax Post in a Controller's Action Method?

查看:98
本文介绍了如何在控制器的操作方法中重定向Ajax发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ajax POST发送到我的一个控制器.例如

I have an Ajax POST being sent to one of my Controller. e.g.

查询>索引.

在Index()操作中,我返回JSON的ActionResult.

In the Index() Action, I return an ActionResult of JSON.

在进行登录检查时,我会执行一条if语句,并且如果activeUser的会话令牌为null,那么我将执行

As I am doing login checks, I do an if statement and if the activeUser's session token is null, I do a

return RedirectToAction("Index","Auth");

但是,这不能正确重定向,而是以浏览器抱怨的JSON返回结果.

However, this is not redirecting properly but instead is returning the result in JSON which the browser complains about.

如何正确设置重定向,以便如果IF语句失败,它将转到登录页面,否则将返回我需要的JSON结果?

How do I set a redirect properly such that if the IF statement fails, it will go to the login page but otherwise it will return the JSON results that I require?

推荐答案

return Json(new{data=true})

关于ajax成功方法

 $.ajax({
            type: 'POST',
            url: 'your url',
            data:your data
            success: function (data) {
                if (data) {
                    window.location.href="url";
                }
                else {
                     window.location.href="url";
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
        return false;

这篇关于如何在控制器的操作方法中重定向Ajax发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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