views:

378

answers:

1

Hello,

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

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"));

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

Thanks,
Stian

A: 

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);

In JavaScript I could then use

  XMLHttpRequest.getResponseHeader("Location")

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

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

Stian