为什么servlet中的response.sendRedirect()在收到JQuery的post请求后不起作用?

mac*_*ers 5 html java jquery redirect servlets

在blog-edit.html中,JQuery用于将post请求发送到服务器端(java servlet).

$("#btn").click(function() {
                    $.post("/blog/handler",{"content":$('#textarea').val()},
                    function(data){
                        alert("Data Loaded: " + data);
                        if(data.toString().length>1){
                            alert("Saved!")
                        }else{
                            alert("Failed!")
                        }
                    })
Run Code Online (Sandbox Code Playgroud)

在服务器端:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String content = request.getParameter("content");
            System.out.println(content);

            response.sendRedirect("/blog/list");
            return;
    }
Run Code Online (Sandbox Code Playgroud)

我看到的是服务器端正在从html打印内容,并弹出警报窗口说"已保存!".但重定向功能不起作用

搜索后我别无选择,只能使用jquery重定向:

if(data.toString().length>1){
                            alert("Saved!")
                            window.location.replace("/blog/list")
                        }
Run Code Online (Sandbox Code Playgroud)

它有效,但它不是我想要的

请帮忙

Raa*_*aab 7

在使用ajax时.你不能执行服务器端重定向.

但是,在这种情况下,如何在客户端上重定向有更好的方法.

看这里