在获得新令牌之前阻止ajax请求 [英] Prevent ajax request till new token is obtained

查看:147
本文介绍了在获得新令牌之前阻止ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的WEB API实现了刷新令牌功能,因此用户可以使用刷新令牌来获取新令牌.

I have implemented refresh token functionality for my WEB API due to which a user can get a new token using a refresh token.

每当API返回未经授权的状态代码时,我都需要获取新令牌并使用该新令牌更新所有待处理的请求.但是相反,一些ajax请求发送带有旧令牌的API请求,因为尚未获得新令牌,并且新令牌的请求正在进行中.

Whenever API returns unauthorized status code, I need to get new token and updated all the pending request with that new token. But instead of that, some ajax request send API request with old token as new token is not obtain yet and the request of new token is in progress.

我想要一种方法,直到没有获得新令牌之前都可以阻止该请求.另外,我已经使用了第三方javascript库,因此无法将async设置为false,因为这会损害系统的性能.

I want a way to prevent this request till new token is not obtain. Also, I have used a third party javascript library so I cannot set async to false as this will harm the performance of system.

更新:

通过添加以下逻辑解决了该问题.希望这可以帮助其他人.

Resolved the issue by adding below logic. Hope this can help others.

$.ajaxPrefilter(function (opts, originalOpts, jqXHR) {
// you could pass this option in on a "retry" so that it doesn't
// get all recursive on you.
if (opts.refreshRequest) {
    return;
}

// our own deferred object to handle done/fail callbacks
var dfd = $.Deferred();

// if the request works, return normally
jqXHR.done(dfd.resolve);

// if the request fails, do something else
// yet still resolve
jqXHR.fail(function () {
    var args = Array.prototype.slice.call(arguments);
    if (jqXHR.status === 401) {
        $.ajax({
            url: BASEPATH.APIPATH + '/Token',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
            },
            method: 'POST',
            refreshRequest: true,
            error: function () {
                // reject with the original 401 data and then redirect to login page.
                setTimeout(function () {
                    window.location = "/Login";
                }, 4000);
                dfd.rejectWith(jqXHR, args);
            },
            success: function (res) {
                // retry with a copied originalOpts with new access token.
                var newOpts = $.extend({}, originalOpts, { url: opts.url });

                // pass this one on to our deferred pass or fail.
                $.ajax(newOpts).then(dfd.resolve, dfd.reject);
            }
        });

    } else {
        dfd.rejectWith(jqXHR, args);
    }
})

return dfd.promise(jqXHR);});

推荐答案

使用以下代码在特定时间内使用javascript,直到服务器生成新令牌.

Engage your javascript with below code for specific time until server generate new token.

function wait(ms) {
    var start = new Date().getTime();
    var end = start;
    while (end < start + ms) {
        end = new Date().getTime();
    }
}

wait(5000); //5秒

wait(5000); //5 seconds

在等待功能中,您可以指定服务器生成令牌所花费的时间.请投票对您有帮助.

In wait function, you can specify the time taken by the server to generate token. Please vote if it will help you.

这篇关于在获得新令牌之前阻止ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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