Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will redirecting before an Ajax call returns cause issues?

Tags:

html

jquery

ajax

I have an ajax call where a redirect happens after the call returns (inside the success handler):

$.ajax({
        type: "POST",
        url: "/LoadSomeData",
        dataType: "json",
        success: function (response) {  
            location.href = "http://mysite.com";                
        }
    });

If I do a redirect before the call returns, will the ajax call get cancelled? For example:

 $.ajax({
            type: "POST",
            url: "/LoadSomeData",
            dataType: "json",
            success: function (response) {  
                location.href = "http://mysite.com";                
            }
        });
 location.href = "http://mysite.com";    

I have a situation where I don't really care to wait till all my data is loaded, and I'd like to redirect immediately. Question is will redirecting (possibly before the loading has completed) disrupt the loading process?

Thanks...

like image 565
Prabhu Avatar asked Jun 26 '13 06:06

Prabhu


People also ask

Does ajax follow redirect?

ajax appears to always follow redirects.

What triggers ajax success?

What triggers Ajax success? Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the . ajaxSuccess() method are executed at this time.

What does an ajax call return?

ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.

Are ajax calls blocking?

ajax is asynchronous (i.e. non-blocking) by default, that's what the A in Ajax means.


1 Answers

If you redirect outside ajax callbacks, the callbacks will be destroyed and you will redirect immediately. This should have no impact on loading process of the redirected address.

like image 51
Majid Fouladpour Avatar answered Oct 11 '22 19:10

Majid Fouladpour