二、MVC+EF控制器和后台交互

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36792339/article/details/82840171

一、ajax 请求的控制器 里面 Redirect 是无效的 表单可以 :
好像 ajax 不管 get还是post 都是跳转不了的
至今 还不知道具体原因
(ajax是一个容器,用来实现像网页一样获取数据的,就是重定向了也不会改变你当前的浏览器页面。)

不只是.NET 有这个问题 java也有 这个锅 .net不背
在这里插入图片描述

Login页面 要跳转到 index页面


@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        $(function(){
            $('.submit-btn').click(function () {
                $.ajax({
                    url: '/Login/Login',
                    type:'post',
                    //dataType:'json',
                    timeout:1000,
                    success: function (data, status) {
                        location.href = data;
                        console.log(data)
                    },
                    fail: function (err, status) {
                        console.log(err)
                    }
                });
            });
        });
    </script>

    <div class="submit-btn"> 
       登陆
    </div>
</body>

</html>

Login 控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LawOA.UserWeb.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();  //1、页面展示
        }
        //[HttpPost]
        public ActionResult Login()
        {
            return Content("/Index/Index");
            //return Content("<script>location.href='/Index/Index';</script>", "text/html");
            //return Redirect("/Index/Index");  //1、ajax 请求 然后跳转 index控制器 index action
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_36792339/article/details/82840171