我试图从Servlet进行页面重定向但是它不起作用我不知道为什么,如果有人可以帮助我会感激,我花了2个小时搜索但是我没有得到结果。
JS代码
$("#login_form_submit").click(function(){
var form = $('#login_form');
$.ajax({
type: "Post",
url: "AccountServlet",
data: form.serialize(),
dataType: "json",
//if received a response from the server
success: function( data, textStatus, jqXHR) {
if(data.success){
$("#ajaxResponse").html("");
console.log(data);
if (data.User.role == 1){
main.user = data.User;
//window.location.href = "Student_home.jsp";
}
}
//display error message
else {
$("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>");
}
},
//If there was no response from the server
error: function(jqXHR, textStatus, errorThrown){
$("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>");
$("#ajaxResponse").html(jqXHR.responseText);
},
//capture the request before it was sent to server
beforeSend: function(jqXHR, settings){
//disable the button until we get the response
$('#login_form_submit').attr("disabled", true);
},
//this is called after the response or error functions are finsihed
//so that we can take some action
complete: function(jqXHR, textStatus){
//enable the button
$('#login_form_submit').attr("disabled", false);
}
});
});
servlet代码
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Max-Age", "86400");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
DatabaseWrapper context = new DatabaseWrapper();
User user = context.Check_login(userName, password);
session.current_user = user;
Gson gson = new Gson();
JsonObject myObj = new JsonObject();
if (user == null){
myObj.addProperty("success", false);
out.println(myObj.toString());
}else if (user instanceof Student){
JsonElement userObject = gson.toJsonTree((Student)user);
myObj.addProperty("success", true);
myObj.add("User", userObject);
out.println(myObj.toString());
out.close();
//response.setStatus(HttpServletResponse.SC_FOUND);
response.sendRedirect("Student_home.jsp");
return;
}
我也尝试使用
RequestDispatcher dispatcher = request.getRequestDispatcher("/Student_home.jsp");
dispatcher.forward(request, response);
但它没有成功,任何建议!
只使用javascript和我一起工作正常,但我需要从servlet重定向!
我在javascript中使用了这一行
window.location.href = "Student_home.jsp";
伙计们我找到了答案,但出于某种原因,我无法将其发布在解决方案中?!
问题是我重定向ajax调用并且没有办法阻止它,这个想法是来自jquery成功的响应我将使用另一个创建表单元素的调用重定向并提交它以自然的方式
我的解决方案:
function Redirect (servlet , method_type){
var form = $(document.createElement('form'));
$(form).attr("action", servlet);
$(form).attr("method", method_type);
$(form).css("display", "none");
form.appendTo( document.body );
$(form).submit();
};
它完成了,谢谢每一个人。
答案 0 :(得分:0)
关闭()后你无法做任何回应。
说到标题,你甚至无法在提交响应后写出它们。在你的情况下,如果你正在重定向,你甚至不应该写任何回复。
这同样适用于RequestDispatcher.forward()。在转发之前,您不应该在响应中写入任何内容。并且,永远不要关闭()。