java 为什么servlet中的response.sendRedirect()在收到JQuery的post请求后不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10712612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why the response.sendRedirect() in servlet doesn't work after receiving the post request of JQuery?
提问by macemers
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!". But the redirect function doesn't work
我看到的是服务器端正在打印 html 中的内容,并且弹出警告窗口说“已保存!”。但是重定向功能不起作用
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
请帮忙