-2

I just want to know if I can redirect a user through ajax call where i have to call one of my method in java and then do a redirection from there

Vinayjava
  • 171
  • 2
  • 9
  • I don't think Java *has* AJAX calls. Did you mean Javascript or are you actually referring to back-end Java? – Waleed Khan Mar 11 '13 at 13:45
  • Looks pretty obvious to me that he is referring to back-end Java. – Michael De Keyser Mar 11 '13 at 13:48
  • see theses links once : http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call , http://stackoverflow.com/questions/7075999/redirecting-main-window-from-ajax-call , http://stackoverflow.com/questions/9149354/php-redirection-headers-not-sent-through-ajax-call , – Hussain Akhtar Wahid 'Ghouri' Mar 11 '13 at 13:52

3 Answers3

6

AJAX calls return raw data to a Javascript callback.
They cannot tell the browser to do anything.

Instead, you can tell your Javascript code to navigate to a new page.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

You can build an ajax redirect (for example in a filter in doFilter) like that.

String facesRequestHeader = httpServletRequest.getHeader( "Faces-Request" );

boolean isAjaxRequest = facesRequestHeader != null
        && facesRequestHeader.equals( "partial/ajax" );

String url = MessageFormat.format( "{0}://{1}:{2,number,####0}{3}",
        request.getScheme(), request.getServerName(),
        request.getServerPort(), YOUR_NEW_URL);

PrintWriter pw = response.getWriter();
pw.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
pw.println( "<partial-response><redirect url=\"" + url
        + "\"></redirect></partial-response>" );
        pw.flush(););

This is in case you are using servlets or JSF or something like that, and you can redirect before going to the lifecycle.

Oscar Castiblanco
  • 1,626
  • 14
  • 31
0

You can send an ajax response from Java then redirect the user following the answer.

Michael De Keyser
  • 787
  • 1
  • 17
  • 45