Why does response.sendRedirect () in the servlet not work after receiving a jQuery mail request?

In blog-edit.html, jQuery was used to send a server-side send request (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!") } }) 

On 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; } 

I saw that the server side is printing content from html, and a warning window appears to say "Saved!". But the redirect function does not 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 that is not what i want

please, help

+4
source share
1 answer

When using ajax. you cannot perform server-side redirection.

However, there is a better way to redirect to the client in such a scenario.

Look here

+7
source

Source: https://habr.com/ru/post/1413871/


All Articles