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

一、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
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力吧少年-珊珊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值