jeetZ
jeetZ

Reputation: 65

How to recall an ajax on failure?

Here is my ajax call

 $.ajax({
        url: "getProduct",
        type: "POST",
        dataType: "xml",
        data: {
            productid: $("#serviceequip1").val()
            },
        error: function(){  
        },
        success: function(xml){   
            $(xml).find("product").each(function()
            {

                //do something.. 
            });


        }
    });

I have checked in development mode of browser, sometimes it shows 505 error for ajax call.

Please suggest some solutions.

Upvotes: 1

Views: 3151

Answers (2)

legendJSLC
legendJSLC

Reputation: 436

Define a function to do ajax call. When error, call the function again.

 function getProduct(){
 $.ajax({
        url: "getProduct",
        type: "POST",
        dataType: "xml",
        data: {
            productid: $("#serviceequip1").val()
            },
        error: function(){ 
                setTimeout(getProduct, 200);
        },
        success: function(xml){   
            $(xml).find("product").each(function()
            {
                //do something.. 
            });
        }
    });
  };

Upvotes: 1

if-else-switch
if-else-switch

Reputation: 977

Just write this ajax call inside a method and in the failure block of ajax just call the method again.

function ajaxCall(){
 $.ajax({
        url: "getProduct",
        type: "POST",
        dataType: "xml",
        data: {
            productid: $("#serviceequip1").val()
            },
        error: function(){  
ajaxCall();
        },
        success: function(xml){   
            $(xml).find("product").each(function()
            {

                //do something.. 
            });


        }
    });
}

Upvotes: 0

Related Questions