Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a redirect in ajax request?

Tags:

jquery

ajax

http

I want to use jQuery to GET a URL and explicitly check if it responded with a 302 redirect, but not follow the redirect.

jQuery's $.ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it?

There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.

like image 446
kdt Avatar asked Feb 07 '12 13:02

kdt


People also ask

Do Ajax calls follow redirects?

ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it? There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.

How do I redirect to another page in Ajax?

$. ajax({ type: 'POST', url: 'AJAX URL', data: "YOUR DATA" // don't forget to pass your csrf_token when using post success: function(data){ $("what_ever_you_want_to_replace"). html(data. view); }, error: function(xhr, type, exception) { // if ajax fails display error alert alert("ajax error response type "+type); } });

How redirect Ajax in PHP?

If you want to do a full redirect, you can use window. location = 'addcust. php? new_sale='+youridvariable In the success callback.

What is success function Ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


1 Answers

The AJAX request never has the opportunity to NOT follow the redirect (i.e., it must follow the redirect). More information can be found in this answer https://stackoverflow.com/a/2573589/965648

like image 137
Nick Garvey Avatar answered Sep 18 '22 11:09

Nick Garvey