重试AJAX请求将忽略成功回调 [英] Retrying AJAX request ignores the success callback

查看:119
本文介绍了重试AJAX请求将忽略成功回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果令牌过期,我正在尝试刷新AJAX调用.使用ajaxSetup一切正常,但是请求忽略success回调.

I am trying to refresh an AJAX call if the token is expired. Everything works fine by using ajaxSetup but the request ignores the success callback.

$.ajax({
  url: _url
  //async: false,
  type: 'POST'
}).success(function(jsonP) {
  console.log(jsonP);
}).error(
  // ...
);

$.ajaxSetup({
  tryCount: 0,
  retryLimit: 2,
  error: function(xhr, textStatus, errorThrown) {
    if (xhr.status == 401 && (JSON.parse(localStorage.getItem('Auth')).user[0] != null)) {
      refreshToken();
      var request = this;
      request.tryCount++;
      setTimeout(function() {
        if (request.tryCount <= request.retryLimit) {
          //try again
          $.ajax(request);
          $('#loading-modal').modal('hide');
        } else {
          alert("Problème de connexion");
        }
      }, 1000);
      return;
    } else {
      /* alert("Problème d'authentification");
      $(".logout").trigger('click'); */
    }
    if (xhr.status == 500) {
      //handle error
    } else {
      //handle error
    }
  }
});

推荐答案

它们是$ .ajax()中的错误;句法.当我运行该代码时,它说成功不是功能.所以我在$ .ajax语法以下也纠正了这个问题:

Their was an error in $.ajax(); syntax. When I ran that code it says success is not a function. So I corrected that too below $.ajax syntax:

JS代码:

方法1 :参考链接: http://api.jquery .com/jquery.ajax/

$.ajax({
    url: "https://www.google.co.in/abc.php", // _url, for time being replaced _url with another url
    //async: false,
    type: 'POST'
}).done(function(jsonP) {
    console.log("jsonP=", jsonP);
}).fail(function() {
    // ...
    console.log("Error Occurred");
});

OR

方法2 :使用成功的旧风格& $ .ajax中的错误函数

Method 2: Old style of using success & error functions in $.ajax

$.ajax({
    url: "https://www.google.co.in/abc.php", // _url, for time being replaced _url with another url
    //async: false,
    type: 'POST',
    /*error: function() {
        console.log("Error occurred");
    },*/
    success: function(jsonP) {
        console.log("jsonP=", jsonP);
    }
});

还有一点需要注意的是,如果$ .ajax()请求包含其自己的错误"处理函数,则它将忽略以$ .ajaxSetup方法编写的错误函数.

Also one more note, if $.ajax() request contains it's own "error" handler function then it ignores the error function written in $.ajaxSetup method.

$.ajaxSetup({
    tryCount: 0,
    retryLimit: 2,
    error: function(xhr, textStatus, errorThrown) {
        if (xhr.status == 401) {
            refreshToken();
            var request = this;
            request.tryCount++;
            setTimeout(function() {
                if (request.tryCount <= request.retryLimit) {
                    //try again
                    $.ajax(request);
                    $('#loading-modal').modal('hide');
                } else {
                    alert("Problème de connexion");
                }
            }, 1000);
            return;
        } else {
            // commented existing code for testing purpose only
            /* alert("Problème d'authentification");
            $(".logout").trigger('click'); */

            // for time being I had added here refresh ajax code to verify whether it's working fine or not
            var request = this;
            request.tryCount++;
            setTimeout(function() {
                if (request.tryCount <= request.retryLimit) {
                    //try again
                    $.ajax(request);
                    // $('#loading-modal').modal('hide');
                } else {
                    alert("Problème de connexion");
                }
            }, 1000);
            return;
        }
        if (xhr.status == 500) {
            //handle error
        } else {
            //handle error
        }
    }
});

与成​​功"回调忽略有关,以确保其正常工作.我刚刚更改了console.log FROM :console.log(jsonP);

Related to "success" callback ignoring, for making it sure it's working or not. I have just changed console.log FROM: console.log(jsonP);

TO :console.log("jsonP =",jsonP);

TO: console.log("jsonP=", jsonP);

成功回调正在运行.我猜这是由于错误的ajax语法无法正常工作.

And that success callback is working. I guess it wasn't been working due to erroneous ajax syntax.

这篇关于重试AJAX请求将忽略成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆