$.ajax({ type: "GET",dataType: "json",url: "url",cache: false,async: false,success: function (data) { alert(data); },error: function(xhr,textStatus,errorThrown) { //$.ajax(this); //return; },statusCode: { 403: function() { var session = retryLogin(); if(session !== "") { // call this function again? How can we achieve this????? } } } });
请让我知道如何再次呼吁那个应该在第一个地方运行的ajax电话?
编辑:
基本上我有两个AJAX调用,即一个用于认证的AJAX调用,用于获取会话ID,另一个是基于该会话ID来获取一些数据.如果会话ID到期中途,我想重新进行身份验证调用,然后继续执行首先要发生的AJAX调用.关于我如何实现的任何建议?
干杯.
解决方法
您可以将ajax调用包装在一个函数中,如果它是一个错误,则再次调用它.
var callMeAgain = function(){ $.ajax({ type: "GET",errorThrown) { callMeAgain(); //text statuses for error are: "timeout","error","abort",and "parsererror" } }); };
这是你想要实现的?
ajax调用有一个超时参数,你可以这样做.
var callMeAgain = function(){ $.ajax({ type: "GET",timeout: 400,success: function (data) { alert(data); },errorThrown) { if (textStatus=="timeout") { callMeAgain(); } } });
正如我在类似的答案中看到的,必须加上:
Set a timeout (in milliseconds) for the request. This will override
any global timeout set with $.ajaxSetup(). The timeout period starts
at the point the $.ajax call is made; if several other requests are in
progress and the browser has no connections available,it is possible
for a request to time out before it can be sent. In jQuery 1.4.x and
below,the XMLHttpRequest object will be in an invalid state if the
request times out; accessing any object members may throw an
exception. In Firefox 3.0+ only,script and JSONP requests cannot be
cancelled by a timeout; the script will run even if it arrives after
the timeout period.
我们假设你在一些消息中写出异常,我们将在成功函数中捕获异常.
var sessionCallVar = function sessionCall () { return $.ajax(...); }; var callMeAgain = function(){ $.ajax({ ...... success: function (response) { if(response.SessionExceptionMessage !== ''){ //then we have a session error sessionCallVar().then(callMeAgain); } },errorThrown) { ......... } });
根据承诺链接ajax电话:How do I chain three asynchronous calls using jQuery promises?
这是我在asp.net中拥抱的某种架构[我不太了解你的服务器端语言]:ASP.NET MVC Ajax Error handling
Ajax错误处理和处理错误回调中的自定义异常:jQuery Ajax error handling,show custom exception messages