【问题标题】:Problems with user verification用户验证问题
【发布时间】:2018-05-14 10:05:51
【问题描述】:

我正在检查用户是否存在,如果存在,我会将他带到主页。
我已成功调用主页面操作方法,但前视图没有改变。

这是我的 AJAX 请求:

    $.ajax({
            type: "GET",
            url: "../../login/login_req",
            dataType: "JSON",
            data: {
                _name: inf.user_name,
                _pass: inf.pass,
                _isadmin:inf.admin_or_not
            },

            complete: function (result) {
                console.log(result);
            }
        });

这是我的控制器:

public class LoginController : Controller
{
    dbhelp dbh = new dbhelp();
    // GET: Login
    public ActionResult Index()
    {
        return View("Login");
    }

    public ActionResult login_req(string _name,string _pass,bool _isadmin)
    {
        ret retu = new ret();
        retu.dt = new System.Data.DataTable();
        if (_isadmin)
        {
           retu= dbh.data_table(string.Format( "select * from users where username='{0}' and password='{1}'",_name,_pass));
        }
        else {
            retu = dbh.data_table(string.Format("select * from employees where e_name='{0}' and e_password='{1}' ", _name, _pass));
        }

        if (retu.dt.Rows.Count > 0)
        {
            return RedirectToAction("Index", "Home");
        }            
    }
}

这是返回成功执行但浏览器中的前视图没有改变的主视图的控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

【问题讨论】:

  • 你调试过代码吗?你到达“return RedirectToAction..”这一行了吗?此外,请确保您使用正确的用户/通行证组合;)
  • 是的,我做了,但什么也没发生,它只是被执行了。对正面没有影响
  • ajax 的全部意义在于留在 SAME 页面 - ajax 调用无法重定向。

标签: c# asp.net asp.net-mvc


【解决方案1】:

遗憾的是,您不能将RedirectToAction 直接返回给 Ajax。要实现你想要的,你应该看看this question

基本上,你需要告诉 js 调用重定向。根据您的具体需求,有很多方法可以做到这一点。

【讨论】:

  • 我使用了 window.location.href = '../../home/index';并且工作
  • @Ricky,在 ajax 调用中使用 location.href 重定向是完全愚蠢的......如果你想重定向,请不要使用 AJAX!
  • @StephenMuecke 这个问题向我表明重定向是有条件的,在这种情况下,没有什么愚蠢的。
  • OP 的问题中的代码没有任何条件(它甚至无法编译)
  • 我必须使用ajax进行验证,并且重定向是有条件的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 2021-11-20
  • 2011-09-20
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多