简体   繁体   English

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

[英]Why the response.sendRedirect() in servlet doesn't work after receiving the post request of JQuery?

In the blog-edit.html, JQuery was used to send the post request to the sever side(java servlet). 在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!")
                        }
                    })

In the server side: 在服务器端:

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

            response.sendRedirect("/blog/list");
            return;
    }

What I saw is the server side is printing the content from the html, and the alert window pops up to say "Saved!". 我看到的是服务器端正在从html打印内容,并弹出警报窗口说“已保存!”。 But the redirect function doesn't work 但重定向功能不起作用

After searching I have no choice but to use jquery to redirect: 搜索后我别无选择,只能使用jquery重定向:

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

it works, but it's not what i want 它有效,但它不是我想要的

please help 请帮忙

While using ajax. 在使用ajax时。 you can not execute server side redirect. 你不能执行服务器端重定向。

However, there are better way how to redirect on client in such a scenario. 但是,在这种情况下,如何在客户端上重定向有更好的方法。

See Here 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Servlet不执行response.sendRedirect(addressPath); ,但会执行没有路径的response.sendRedirect() - Servlet doesn't execute response.sendRedirect(addressPath); , but does execute response.sendRedirect() without path request.getRequestDispatcher和response.sendRedirect不起作用 - request.getRequestDispatcher and response.sendRedirect don't work response.sendRedirect(“ / test.jsp”); 不起作用 - response.sendRedirect(“/test.jsp”); doesn't work response.sendRedirect()没有正确重定向 - response.sendRedirect() doesn't redirect properly 如果在提交response之后调用java servlet:response.sendRedirect(),则不会给出非法状态异常。为什么? - java servlet:response.sendRedirect() not giving illegal state exception if called after commit of response.why? 上传文件后 Servlet 中的 response.sendRedirect() 无法正常工作 - response.sendRedirect() in Servlet not working correctly after file upload 使用 response.sendRedirect 方法在 java 中发送 Post 请求 - Send Post request in java with response.sendRedirect method response.sendRedirect不跟踪ip - response.sendRedirect doesn't keep track of ip Servlet response.sendRedirect编码问题 - Servlet response.sendRedirect encoding problems response.sendRedirect() 没有运行 - Servlet - response.sendRedirect() does not running - Servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM