重定向阿贾克斯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 =我的HTML登录页面无效的JSON -Markup -
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 的类型的结果,但由于重定向自动处理,它将接收的生成html源的你的 login.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.
一个想法是让浏览器知道它应该通过增加一个重定向
变量所得到的对象,并在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...
}
}
//....
希望有所帮助。
Hope that helps.
我的答案在很大程度上受到<一的灵感href="http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call">this螺纹这不应该留在任何情况下,你的问题还是有一些问题。
My answer is heavily inspired by this thread which shouldn't left any questions in case you still have some problems.
这篇关于重定向阿贾克斯jQuery的电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!