管理超时ajax请求

亚历山德罗·米诺切里(Alessandro Minoccheri)

我想管理与超时Ajax调用.when.then

我有一个像这样的脚本:

$.when(
        $.ajax({   
            url: url_ajax, 
            type: "GET", 
            async: true,
            data: window.location.search, 
            dataType: "json", 
            timeout: 30000,
            success: function(data) {
                console.log('ok');
            },
            error: function(data) {

            }
        }),
        $.ajax({   
            url: url_ajax, 
            type: "GET", 
            async: true,
            data: window.location.search, 
            dataType: "json", 
            success: function(data) {
                console.log('ok');
            },
            timeout: 30000,
            error: function(data) {

            }
        })
     ).then(function() {
          alert('end');
     });

如果ajax请求进入超时,.then则不会调用该回调,并且我也看不到最后一个警报。因此,如果我的ajax请求进入超时,我将无法退出进入then功能。

我也尝试在每个ajax请求之后添加此代码:

.fail(function(jqXHR, textStatus){
            if(textStatus == 'timeout')
            {     
                alert('Failed from timeout'); 
            }
         })

但是再次没有then起作用

如何管理超时?谢谢

麦坎夫

您是否尝试过.then(successCallbackFunction, failCallbackFunction)像这样使用

$.when(
    $.ajax({   
        url: url_ajax, 
        type: "GET", 
        async: true,
        data: window.location.search, 
        dataType: "json", 
        timeout: 30000,
        success: function(data) {
            console.log('ok');
        },
        error: function(data) {

        }
    }),
    $.ajax({   
        url: url_ajax, 
        type: "GET", 
        async: true,
        data: window.location.search, 
        dataType: "json", 
        success: function(data) {
            console.log('ok');
        },
        timeout: 30000,
        error: function(data) {

        }
    })
 ).then( // success
         function() {
           alert('Success!');
         },
         // failure
         function(jqXHR, textStatus){
            if(textStatus == 'timeout')
            {     
                alert('Failed from timeout'); 
            } else {
                alert('Failed from: '+textStatus);
            }
         }
 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章