AJAX - Get response url after redirect

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

I want to use this with a form that is at "mysite.com/users/add.html". This is a Spring MVC controller. If form validation fails, the controller will return the users / add view, but if validation is correct, it will 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

+2


a source to share


1 answer


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

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

      

In JavaScript, I could use



  XMLHttpRequest.getResponseHeader("Location")

      

Hopefully it doesn't cause any unexpected problems.: P

If anyone has an easier solution though I would love to see one.

+2


a source







All Articles