AJAX - 获取重定向后响应URL [英] AJAX - Get response URL after redirect

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

问题描述

我在想,如果有一种方法在JavaScript AJAX调用?

I was wondering if there is a way to get the URL of the returned resource after a AJAX call in JavaScript?

我想用这个带有一种形式,这是在mysite.com/users/add.html。这是一个Spring MVC的控制。如果表单的验证失败,控制器将返回用户/添加的说法,但如果验证OK它会做一个重定向到mysite.com/users/index.html,使用

I want to use this with a form, which is in "mysite.com/users/add.html". This is a Spring MVC controller. If the validation of the form fails the controller will return the "users/add" view, but if the validation is OK it will do a redirect to "mysite.com/users/index.html", using

return new ModelAndView(new RedirectView("/users/index.html"));

有没有办法在JavaScript中找到返回的页面的网址是什么?

Is there a way in JavaScript to find the URL of the returned page?

谢谢,
了Stian

Thanks,
Stian

推荐答案

在一个Java过滤器设置响应头位置解决了这个。

Solved this by setting the response header "Location" in a Java Filter.

  HttpServletRequest httpRequest = (HttpServletRequest) request;
  HttpServletResponse httpResponse = (HttpServletResponse) response;
  String path = httpRequest.getRequestURI();
  httpResponse.setHeader("Location", path);

在JavaScript中,我可以再使用

In JavaScript I could then use

  XMLHttpRequest.getResponseHeader("Location")

希望这不会造成任何不可预见的问题。 :P

Hopefully this won't cause any unforeseen problems. :P

如果任何人有一个简单的解决方案,虽然我想看到的。

If anyone else have an easier solution though I'd like to see it.

这篇关于AJAX - 获取重定向后响应URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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