Servlet Redirect in Ajax Request

Hi team,

I wanna redirect the request that contains “X-Requested-With: XMLHttpRequest” in the header. I know that response.sendRedirect(url) method is not possible in ajax call, so i tried:

httpRequest.getRequestDispatcher(“myservlet”).forward(req, res);
//or
httpRequest.getRequestDispatcher(“myservlet”).include(req, res);

but the same problem, the client is not redirected to my servlet.

and too i tried to return a xml response with this:

<?xml version="1.0" encoding="UTF-8"?>

but nothing happens.

I’m stuck in this problem for weeks, if have any that can help me i will be very grateful.

Thanks.

Resolved :smiley:

Put this code into the client-side:
function requestUnauthorized(xhr) {
window.location.href = xhr.getResponseHeader(“Location”);
}
(function($) {
$.ajaxSetup({
statusCode: {
401: requestUnauthorized,
}
});
})(window.jQuery);

And all request can be redirect to another page.