I can not provide a detailed response now, but I can say that it is impossible to detect in Ajax or normal requests the redirection, the only one that knows that there was redirection is the interface of the browser's internal requests.
It would be something like:
UserInterface:
Itistherenderedlayer,whereyoualreadyhavethereadyorpartialresponse,thebrowserscreen(knownaswebView)
Requestsandresponsesinterface:
ItisaninternalinterfacethatonlyrespondstothebrowserandmanagesallHTTPrequests,isresponsibleforsolvingaserver,sendingtherequesttoitandpickinguptheresponse,incaseofredirectionitsendsanewrequest.
HTTPServer:
Itisasite,domain,localornot,whichisaccessiblethroughanaddressanditsanswersareheadersfollowedbytextsorbinarydatathat"do nothing" (since the responsible for interpreting this is the interface of requests and responses) .
When a redirect occurs with 301
, 302
, etc., who will manage this is the Request and Answer Interface , the only tool that sees this is the browser debugger press the F12 keypad that will display the debug).
How to solve
The only way is to change the approach, ie instead of redirecting in the back end, switch to the front end and do something similar to what you put in your own response, json or xml responses with "commands" that say what to do and even create your own custom error codes, like this:
-
Error code example (HTTP response will always be 200 Ok
)
{ "status": 5000, "message": "Erro na requisição" }
-
Success code example
{ "status": 1000, "message": "Cadastrado com sucesso" }
-
Sample redirect code on the front end
{ "status": 9000, "message": null, "url": "/path/route" }
The code would look something like:
var request = $.ajax({
url: "/route/page",
method: "GET",
data: { id : 1 },
dataType: "json"
});
request.done(function(data) {
if (data.status === 9000) {//Detecta redirecionamento
window.location = data.url; //Redireciona (se quiser :) )
} else {
//...Código segue fluxo normal
}
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
Note: I think that maybe you have thought about making a request on the server, it would work, but I think that besides being more of a gambiarra, it would be very costly for the server as the number of users increased accessing at the same time