重新发送ajax请求,如何重新发送失败的ajax请求?

在这种情况下,我会为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将返回执行您传入的方法的回调。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值