Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat jQuery ajax request

I make an ajax request to the server. And sometimes I receive 502 error. So, if that happened error() method is called.

How can I repeat request if receive an error? The code should looks like this:

$.ajax({
                url: 'http://server/test.php',
                type: 'GET',
                dataType: 'jsonp',
                cache: 'false',
                timeout: 32000,
                success: function(data) {
                  //some actions here
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log("Error[refresh]: " + textStatus);
                    console.log(jqXHR);
                    // here I want to repeat the request like "this.repeat()"
                },
            });
like image 673
Dmitry Belaventsev Avatar asked Dec 06 '22 16:12

Dmitry Belaventsev


2 Answers

you can do it like this,

function ajaxCall(){
      $.ajax({
                url: 'http://server/test.php',
                type: 'GET',
                dataType: 'jsonp',
                cache: 'false',
                timeout: 32000,
                success: function(data) {
                  //some actions here
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log("Error[refresh]: " + textStatus);
                    console.log(jqXHR);
                    ajaxCall(); // recursion call to method.
                },
            });
}
like image 180
dku.rajkumar Avatar answered Dec 10 '22 13:12

dku.rajkumar


Put your code in function and call this function again. like:

function ajaxFunction()
{
 ....
error:function(){ajaxFunction();}
}
like image 30
Hadas Avatar answered Dec 10 '22 13:12

Hadas