0

I am having an ajax call that needs to run and if it fails then it should reinitialze the same ajax. As i have gone through some post there is failure function under where we can tell user that ajax call failed and follow some set of function.

But i wish to reintialiaze the same ajax call in failure or somewhere else so that when that ajax fails so in fails it intilized again.

deleteImage : function(objId,thumburl,original,ev){

    jQuery('#imageBlocksDiv').append(jQuery("#confirmboxtmp").html());
    setToCenterOfParent( $('#confirmbox'), document.body, false, true );
    jQuery("#confirmbox").fadeIn().css({top:-210,position:'fixed'}).animate({top:50}, 100, function() {});
    jQuery('#deleteconfirmbox').click(function(){
        jQuery(this).text('Deleting..');
        if(typeof objId!='undefined'){

            var iputadatatologdelete = {
                "media_image_objectId": objId,
                "action" : "deleted"    
            };
            iputadatatologdelete.company_objectId = jQuery('#cid').val();
            iputadatatologdelete.edited_by_user_objectId = jQuery('#uid').val();

            var inputData = {
            'id' : objId,
            'imgurl' : original,
            'thumburl' : thumburl
            }

            jQuery.ajax({
                'type':'POST',
                'url': '#####/a###pi/###/####/#####',
                'data' : inputData,
                success : function(response){ //console.log(response)
                    jQuery('#'+objId).parents().eq(2).remove();
                    console.log(objId);
                    jQuery('#confirmbox').hide();
                    jQuery('#deleteconfirmbox').text('Delete');
                    pinterest(undefined);
                    logdata("sc_media_image_edited_log", iputadatatologdelete)
                }
            });
 }

The function is something like this if i go to make function for every kind of ajax that i am calling then that will be bunch of codes. As i have already made loads of ajax with diffrent urls and type.

    error : function(xhr, textStatus, errorThrown ) {

            //try again
            $.ajax(this);
            return;

}

Will this work in case of reinitializing the same query.

5 Answers5

2

You can do something like this:

function doAjax() {
  $.ajax({
    url: ...
    success: ...
    error: doAjax
  });
}

doAjax();

Note, that in case of continuous failure, this will infinitely loop unless you implement a protection mechanism (e.g. a counter and a condition to stop the loop after a number of failed attampts).

marekful
  • 14,986
  • 6
  • 37
  • 59
2

To call exactly the same ajax, you can make it recursively, something like this:

function callAjax(){
  $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    callAjax();
  })
  .always(function() {
    alert( "complete" );
  });
}

Otherwise you can control it with a "control variable", like this:

function callAjax(){
  var control = 0;
  while (control === 0){
      $.ajax( "example.php" )
      .done(function() {
        alert( "success" );
        control = 1;
      })
      .fail(function() {
        alert( "fail" );
      })
      .always(function() {
        alert( "complete" );
      });
  }
}

I hope it helps you.

Wellington Zanelli
  • 1,894
  • 3
  • 18
  • 43
1

Try below code:

$(document).ready(function(){    
    (function callAjax(){
        $.ajax({ 
            url: url,             
            success: function(res){ },            
            complete: callAjax            
        });
    })();

})

You can use "complete" function for that because you either get success or error complete function will call in each case.. http://api.jquery.com/jquery.ajax/

kwelsan
  • 1,229
  • 1
  • 7
  • 18
1

In error callback , call the the same ajax request method

Dibu
  • 891
  • 5
  • 16
0

As in the failure function we can reset the same ajax call by below lines .

    error : function(xhr, textStatus, errorThrown ) {
                jQuery.ajax(this);
                return;
    },  

If i am some where wrong please give the feedbacks on the same