在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!")
}
})
在服务器端:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String content = request.getParameter("content");
System.out.println(content);
response.sendRedirect("/blog/list");
return;
}
我看到的是服务器端正在打印 html 中的内容,并弹出警告窗口说“已保存!”。但是重定向功能不起作用
搜索后只好用jquery重定向:
if(data.toString().length>1){
alert("Saved!")
window.location.replace("/blog/list")
}
可以,但不是我想要的
请帮忙
最佳答案
关于java - 为什么servlet中的response.sendRedirect()在收到JQuery的post请求后不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712612/