从AJAX帖子重定向 [英] Redirect from an AJAX Post

查看:84
本文介绍了从AJAX帖子重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring控制器中对我的端点进行了AJAX调用.从POST验证信息后,我的控制器将做出重定向决定,是将请求转发到下一个位置,还是将其发送回登录页面.对帖子的回复是正确的,它是302,且 Location 标头设置正确.但是,当页面进行重定向调用时,它将对URL进行OPTIONS调用,然后对GET进行调用,后者仅返回HTML.太好了,我有HTML,但是页面停留在我的JSP页面上,并且从不访问外部URL.我该如何处理?

I have an AJAX call to my endpoint in my Spring controller. After verifying the info from the POST, my controller makes a redirect decision, whether to forward the request to the next location, or send them back to a login page. The response to the post is correct, it's a 302 with the Location header set correctly. However, when the page makes the redirect call, it makes an OPTIONS call to the URL, then a GET call, which just returns the HTML. Great, I have the HTML, but the page stays on my JSP page and never goes to the external URL. How do I manage this?

示例Java代码:

@RequestMapping(value = "/token/{token_code}", method = {RequestMethod.GET})
public void validateToken(HttpServletRequest servletRequest, HttpServletResponse servletResponse, @PathVariable String token_code) {
  //set some servlet request attributes from incoming packet info
  if(isTokenValid(token_code)) {
       servletRequest.getRequestDispatcher(MyConstants.JSP_DEVICE_INFO).forward(servletRequest, servletResponse);
   }
  else {
     servletRequest.getRequestDispatcher(MyConstants.FAILURE_URL).forward(servletRequest, servletResponse);
  }
}

@RequestMapping(value = "/token/tokenRedirect", method = {RequestMethod.POST},headers = "content-type=application/json",consumes = {MediaType.APPLICATION_JSON_VALUE})
public ModelAndView getSession(HttpServletRequest servletRequest,
                                   HttpServletResponse servletResponse,
                                   @RequestBody TokenValidateRequest request)
{
  boolean isValid = verifyCollectedInfo(request);
  if(isValid) {
      servletResponse.setHeader("Location", request.url());
      servletResponse.setStatus(302);

  }
  else {
      servletResponse.setHeader("Location", MyConstants.FAILURE_URL);
      servletResponse.setStatus(302);
  }
}

JSP Ajax调用:

JSP Ajax call:

   $.ajax({
         headers: {
             'accept': 'application/json',
             'content-type': 'application/json'
         },
         type: "POST",
         url: "tokenRedirect",
         context:document.body,
         contentType:"application/json",
         data:JSON.stringify(TokenValidateObject)

     });

因此,当我检查网络流量时,我看到响应的状态设置为302,并且Location标头具有我想要的URL,但它只是获取重定向URL的HTML,实际上并没有切换视图

So when I inspect my network traffic, I see the 302 status is set for the response and the Location header has the URL I want, but it just fetches the HTML for the redirect URL, it doesn't actually switch views

推荐答案

您应尝试使用前端而不是后端进行重定向.Ajax旨在发出一个异步请求,然后可以在前端使用它.

You should try using the front end to redirect instead of the back-end. Ajax is meant to make a asynchronous request which then can be consumed on the front end.

因此,与其在后端进行重定向,不如告诉它 print 重定向位置,并使用前端上返回的数据进行重定向.

So, instead of redirecting in the back end, just tell it print the redirect location and redirect using the returned data on the front end.

$.ajax({
     headers: {
         'accept': 'application/json',
         'content-type': 'application/json'
     },
     type: "POST",
     url: "tokenRedirect",
     context:document.body,
     contentType:"application/json",
     data:JSON.stringify(TokenValidateObject)
     success: function(locationToRedirect){
         window.location = locationToRedirect
     } 
});

这篇关于从AJAX帖子重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆