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