重定向 Ajax Jquery 调用 [英] Redirect on Ajax Jquery Call
问题描述
我是 ajax 的新手,我知道有人已经遇到过这个问题.我有一个建立在 Spring MVC 上的遗留应用程序,它有一个拦截器(过滤器),可以将用户重定向到登录没有会话时的页面.
I am newbie to ajax here and I know somebody would have encountered this problem already. I have a legacy app built on Spring MVC, it has a interceptor(filter) that redirects the user to the login page whenever there is no session.
public class SessionCheckerInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
// check if userInfo exist in session
User user = (User) session.getAttribute("user");
if (user == null) {
response.sendRedirect("login.htm");
return false;
}
return true;
}
}
对于非 xmlhttp 请求,这工作正常..但是当我尝试在我的应用程序中使用 ajax 时,一切都变得奇怪,它无法正确重定向到登录页面.如检查
For non-xmlhttp request, this works fine.. but when I try to use ajax in my application, everything gets weird, it is not able to redirect to the login page correctly. As check the value of the
xhr.status = 200 textStatus = parseError errorThrown = "无效的 JSON - 我的 HTML 登录页面的标记-
xhr.status = 200 textStatus = parseError errorThrown = "Invalid JSON -Markup of my HTML Login Page-
$(document).ready(function(){
jQuery.ajax({
type: "GET",
url: "populateData.htm",
dataType:"json",
data:"userId=SampleUser",
success:function(response){
//code here
},
error: function(xhr, textStatus, errorThrown) {
alert('Error! Status = ' + xhr.status);
}
});
});
我检查了我的萤火虫有一个 302 HTTP 响应,但我不知道如何捕捉响应并将用户重定向到登录页.这里有什么想法吗?谢谢.
I checked on my firebug that there is a 302 HTTP response but I am not sure how to catch the response and redirect the user to the login page. Any idea here? Thanks.
推荐答案
JQuery 正在寻找 json 类型的结果,但是因为重定向是自动处理的,它会收到生成的 htmllogin.htm
页面的源.
JQuery is looking for a json type result, but because the redirect is processed automatically, it will receive the generated html source of your login.htm
page.
一个想法是让浏览器知道它应该通过向结果对象添加一个 redirect
变量并在 JQuery 中检查它来重定向:
One idea is to let the the browser know that it should redirect by adding a redirect
variable to to the resulting object and checking for it in JQuery:
$(document).ready(function(){
jQuery.ajax({
type: "GET",
url: "populateData.htm",
dataType:"json",
data:"userId=SampleUser",
success:function(response){
if (response.redirect) {
window.location.href = response.redirect;
}
else {
// Process the expected results...
}
},
error: function(xhr, textStatus, errorThrown) {
alert('Error! Status = ' + xhr.status);
}
});
});
您还可以在响应中添加标头变量,让浏览器决定重定向的位置.在 Java 中,不要重定向,而是执行 response.setHeader("REQUIRES_AUTH", "1")
,而在 JQuery 中,你就成功了(!):
You could also add a Header Variable to your response and let your browser decide where to redirect. In Java, instead of redirecting, do response.setHeader("REQUIRES_AUTH", "1")
and in JQuery you do on success(!):
//....
success:function(response){
if (response.getResponseHeader('REQUIRES_AUTH') === '1'){
window.location.href = 'login.htm';
}
else {
// Process the expected results...
}
}
//....
希望有所帮助.
我的回答深受this的启发线程不应该留下任何问题,以防您仍然有一些问题.
My answer is heavily inspired by this thread which shouldn't left any questions in case you still have some problems.
这篇关于重定向 Ajax Jquery 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!