重定向 Ajax Jquery 调用

编程入门 行业动态 更新时间:2024-10-27 10:27:09
本文介绍了重定向 Ajax Jquery 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 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 调用

本文发布于:2023-10-10 12:04:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Ajax   Jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!