且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring-mvc ajax请求未显示重定向页面

更新时间:2022-12-07 12:55:07

您的浏览器未遵循重定向,因为您正在使用ajax来访问控制器的重定向方法.您有两种选择:

Your browser is not following the redirect because you're using ajax to hit your controller's redirect method. You have two options:

  1. 完全不使用ajax,让您的浏览器自然地响应重定向状态.

  1. Don't use ajax at all, and let your browser respond naturally to the redirect status.

function redFun() {
    window.location = "/HelloWeb/redirect";
}

  • 查找对XmlHttpRequest的HTTP 302响应,并将window.location更改为响应中Location标头的值:

  • Look for an HTTP 302 response to the XmlHttpRequest, and change window.location to the value of the Location header in the response:

    function redFun() {
        $.ajax({
            type: "GET",
            url: "/HelloWeb/redirect"
        }).fail(function (jqXHR, status, err) {
            if (jqXHR.status === 302) {
                window.location = jqXHR.getResponseHeader('Location');
            }    
        });
    }