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

html, java, jquery, redirect, servlets

Solution

While using ajax. you can not execute server side redirect.

However, there are better way how to redirect on client in such a scenario.

See Here

Problem

In the blog-edit.html, JQuery was used to send the post request to the sever side(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!". But the redirect function doesn't work After searching I have no choice but to use jquery to redirect: ``` if(data.toString().length>1){ alert("Saved!") window.location.replace("/blog/list") } ``` it works, but it's not what i want please help

Original source

Related problems