I can’t provide a detailed answer right now, but I can say that is impossible detect in Ajax or normal requests the redirect, the only one who knows that there has been redirection is the interface of internal browser requests.
It would be something like:
User interface:
It is the rendered layer, where you already have the ready or partial response, the browser screen (known as webView)
Request and response interface:
It is an internal interface that responds only to the browser and manages all HTTP requests, is responsible for resolving a server, send the request to it and pick up the response, in case of redirection it sends a new request.
HTTP server:
It is a website, domain, location or not that is accessible through an address and its responses are headers followed by texts or binary data that "do nothing" (because the responsible for interpreting this is the interface of requests and responses).
When a redirect occurs 301
, 302
, etc, who will manage this is the Request and response interface, the only tool that sees this is the browser debugger (in Chrome press the F12 keyboard 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 tell you what to do and even create your own custom error codes, thus:
Error code example (HTTP response will always be 200 Ok
)
{ "status": 5000, "message": "Erro na requisição" }
Example of success code
{ "status": 1000, "message": "Cadastrado com sucesso" }
Example of front-end redirect code
{ "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 maybe you were thinking of making a request on the server, it would work, but I think that besides being more of a gambiarra, it would be quite expensive for the server as it increased the number of users accessing at the same time
Do you have any sample code, friend? It’s a little difficult to understand your scenario exactly
– Victor Alencar Santos
@Wallacemaxters, I believe it is not possible to detect if the server redirected your AJAX request, what can be done is to add a Header on the server informing the processed URL, so you compare this header with the sent url...
– Tobias Mesquita
Our @Victoralencarsantos needs to be more specific? How do I demonstrate a code redirection? Everything I could explain is already in the question
– Wallace Maxters
One way to circumvent this is to send the ajax request to your own server. Then on the server side CURL would do the request. The logic in this is that CURL detects redirects. The problem is the consumption of data traffic and processes on the server just to do this. But anyway, if it’s something so important, there’s a functional gambit.
– Daniel Omine
@Danielomine’s a good move. But in my case to do this would almost in the same, would be the system making request to the same system to then return to the client.
– Wallace Maxters