4

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

macemers
  • 2,194
  • 6
  • 38
  • 55

1 Answers1

7

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

Community
  • 1
  • 1
Raab
  • 34,778
  • 4
  • 50
  • 65