AJAX - 获取重定向后响应URL重定向、AJAX、URL

2023-09-10 13:37:30 作者:愿枕星河入梦

我在想,如果有一种方法在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.