在这种情况下,我会为403状态代码编写一个特定的处理程序,这意味着未经授权(我的服务器也将返回403)。从jquery ajax docs,你可以做到

$.ajax({

statusCode: {

403: function() {

relogin(onSuccess);

}

}

});

实现这一点。

在该处理程序中,我将调用relogin方法,传递一个捕获登录成功时要执行的操作的函数。在这种情况下,您可以传入包含要再次运行的调用的方法。

在上面的代码中,relogin应该调用登录代码,onSuccess应该是一个包装你每分钟执行的代码的函数。

编辑 - 基于您在评论中的澄清,这种情况发生在多个请求中,我个人会为您的应用创建一个API,用于捕获与服务器的交互。

app = {};

app.api = {};

// now define all your requests AND request callbacks, that way you can reuse them

app.api.makeRequest1 = function(..){..} // make request 1

app.api._request1Success = function(...){...}// success handler for request 1

app.api._request1Fail = function(...){...}// general fail handler for request 1

/**

A method that will construct a function that is intended to be executed

on auth failure.

@param attempted The method you were trying to execute

@param args The args you want to pass to the method on retry

@return function A function that will retry the attempted method

**/

app.api.generalAuthFail = function(attempted, args){

return function(paramsForFail){ // whatever jquery returns on fail should be the args

if (attempted) attempted(args);

}

}

因此,使用该结构,您可以在request1方法中执行类似的操作

$().ajax({

....

statusCode: {

403: app.api.generalAuthFail(app.api.request1, someArgs);

}

}}

generalAuthFailure将返回执行您传入的方法的回调。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐