【问题标题】:Redirect on Ajax Jquery Call重定向 Ajax Jquery 调用
【发布时间】:2011-02-25 00:05:32
【问题描述】:

我是这里的 ajax 新手,我知道有人已经遇到过这个问题。 我有一个基于 Spring MVC 构建的遗留应用程序,它有一个拦截器(过滤器)将用户重定向到登录 没有会话时的页面。

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 时,一切都变得很奇怪, 它无法正确重定向到登录页面。 作为检查的值

xhr.status = 200 textStatus = parseError errorThrown = "无效的 JSON - 我的 HTML 登录页面的标记-

$(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 响应,但我不确定如何捕获响应并将用户重定向到登录 页。这里有什么想法吗?谢谢。

【问题讨论】:

标签: jquery ajax spring-mvc


【解决方案1】:

JQuery 正在寻找一个 json 类型的结果,但由于重定向是自动处理的,它会收到您的login.htm 页面的生成的 html 源。 p>

一个想法是让浏览器知道它应该通过将redirect 变量添加到结果对象并在 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 中,您会执行成功(!):

//....
        success:function(response){ 
            if (response.getResponseHeader('REQUIRES_AUTH') === '1'){ 
                window.location.href = 'login.htm'; 
            }
            else {
                // Process the expected results...
            }
        }
//....

希望对您有所帮助。

我的回答深受this thread 的启发,如果您仍有问题,请不要留下任何问题。

【讨论】:

  • 嗨,moonground..谢谢,我想我已经明白了你的意思了这个例子......非常感谢......
  • 谢谢托马斯。我有点明白了,但我要多读一些你引用的线程,这样它就可以稍微下沉。尽管如此,这似乎是我正在寻找的答案,所以感谢一百万!
  • 如何将重定向变量添加到响应中?
【解决方案2】:

对于 ExpressJs 路由器:

router.post('/login', async(req, res) => {
    return res.send({redirect: '/yoururl'});
})

客户端:

    success: function (response) {
        if (response.redirect) {
            window.location = response.redirect
        }
    },

【讨论】:

    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2012-06-09
    相关资源
    最近更新 更多