最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Repeat jQuery ajax request - Stack Overflow

programmeradmin11浏览0评论

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()"
                },
            });

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()"
                },
            });
Share Improve this question asked Jan 22, 2012 at 8:53 Dmitry BelaventsevDmitry Belaventsev 6,65712 gold badges56 silver badges75 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 9

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.
                },
            });
}

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

function ajaxFunction()
{
 ....
error:function(){ajaxFunction();}
}

With calling limit, as suggested by Hadas:

function ajaxCall(count) {
    count = typeof count == 'undefined' ? 0 : count;
    limit = 5;
    if(count === limit) return;
    // no need to specify cache and type:
    //   type has 'GET' as default value
    //   cache has default value of false if the dataType is jsonp
    $.ajax({        
        url: 'http://server/test.php',
        dataType: 'jsonp', 
        timeout: 32000,
        async: false 
    }).done(function(data) {
        // some actions here
    }).fail(function(jqXHR, textStatus, errorThrown) {
        count += 1;
        console.log("Error[refresh]: " + textStatus);
        console.log(jqXHR);
        // 500, 1000, 1500, 2000 etc
        setTimeout(function() {
            ajaxCall(count);
        }, 500*count);
    });
};
发布评论

评论列表(0)

  1. 暂无评论