Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何处理一组延迟';当其中一个失败的时候_Javascript_Ajax_Jquery_Jquery Deferred - Fatal编程技术网

Javascript 如何处理一组延迟';当其中一个失败的时候

Javascript 如何处理一组延迟';当其中一个失败的时候,javascript,ajax,jquery,jquery-deferred,Javascript,Ajax,Jquery,Jquery Deferred,我有一系列ajax调用,它们填充页面上的列 var doneDefers = function(defer) { // leftColDefer is another deferred that sets some header info $.when(defer, leftColDefer).done(function(req1, req2){ var data = req1[0], head = req2[0]; //

我有一系列ajax调用,它们填充页面上的列

var doneDefers = function(defer) {
        // leftColDefer is another deferred that sets some header info
    $.when(defer, leftColDefer).done(function(req1, req2){
        var data = req1[0],
        head = req2[0];
        // spit this data out to elements on the page   

    });
};  
for(i=0;i<window.ids.length;i++){
    defer[i] =  $.ajax({
        url: 'api/get_runs_stats.php',
        type: 'GET',
        dataType: 'json',
        data: {
            run_id: window.ids[i]
        }
    });
    doneDefers(defer[i]);
}
done函数运行良好,没有一个ajax调用失败。如果其中一个失败,我将进入fail函数,而我无法访问其他运行的任何返回数据。arguments数组仅包含失败调用的数据


我想对通过的数据集进行计算。当一个好的调用失败时,我如何从中获取数据?

我不确定这是最简单的解决方案,但它有可能奏效

var ajax_always_promises = [],//to be populated with promises that (barring uncaught error) are guaranteed to be resolved.
    data_arr = [],//an array to be (sparsely) populated with asynchronously delivered json data.
    error_arr = [];//an array to be (sparsely) populated with ajax error messages.

$.each(window.ids, function(i, id) {
    var dfrd = $.Deferred();
    var p = $.ajax({
        url: 'api/get_runs_stats.php',
        type: 'GET',
        dataType: 'json',
        data: {
            run_id: window.ids[i]
        }
    }).done(function(json_data) {
        data_arr[i] = json_data;//intentionally not `data_arr.push(json_data);`
    }).fail(function(jqXHR, textStatus, errorThrown) {
        error_arr[i] = textStatus;//intentionally not `error_arr.push(textStatus);`
    }).always(dfrd.resolve);

    ajax_always_promises[i] = dfrd.promise();
    doneDefers(p);
});

$.when.apply(null, ajax_always_promises).done(function() {
    //The data in the (sparsely) populated arrays `data_arr` and `error_arr` is available to be used. 
    var i, id, success_count=0, error_count=0;
    for(i=0; i<Math.max(data_arr.length,error_arr.length); i++) {

        //Here, the index i corresponds to the original index of window.ids ...
        //...that's the advantage of sparsely populating the arrays.
        id = window.ids[i];
        if(data_arr[i]) {
            //Here, do whatever is required with `data_arr[i]`, and `id` if needed.
            success_count++;
        }
        else if(error_arr[i]) {
            //Here, do whatever is required with `error_arr[i]`, and `id` if needed.
            error_count++;
        }
    }
    console.log("Success:errors: " + success_count + ':' + error_count);
});
var ajax\u always\u promissions=[],//用保证解决的承诺(除非出现未捕获错误)填充。
data_arr=[],//要(稀疏地)填充异步传递的json数据的数组。
错误_arr=[]//要(稀疏地)填充ajax错误消息的数组。
$.each(window.id,function(i,id){
var dfrd=$.Deferred();
var p=$.ajax({
url:'api/get_runs_stats.php',
键入:“GET”,
数据类型:“json”,
数据:{
run\u id:window.ids[i]
}
}).done(函数(json_数据){
data\u arr[i]=json\u data;//故意不使用'data\u arr.push(json\u data)`
}).fail(函数(jqXHR、textStatus、errorshown){
error\u arr[i]=textStatus;//故意不使用'error\u arr.push(textStatus)`
}).始终(dfrd.解决);
ajax_everys_promises[i]=dfrd.promise();
多奈德费尔斯(p);
});
$.when.apply(null,ajax\u始终\u承诺).done(函数()){
//可以使用(稀疏)填充的数组“data_arr”和“error_arr”中的数据。
变量i,id,成功计数=0,错误计数=0;

对于(i=0;我是说如果for循环fail-defer数组中的任何ajax调用都将为空?Barbara,请记住,当其任何承诺参数失败时,
$。when()
将立即启动其失败处理程序,而不必等待其其他承诺参数被解析或失败。
$。when()
不尝试使其故障处理程序可用什么是不保证可用的,这正是您想要的。@在这种情况下,有可能获得具有已解决结果的处理程序吗?或者我需要返回某种全局计数器方案吗?@MinaGabriel调用失败的处理程序,它的参数与我期望的1失败的ajax c的参数匹配全部。它不包括已解析的参数。这有意义吗?如果没有,我可以向问题中添加示例结果。谢谢@Minagariel。如果我读对了,它是每个ajax调用的处理程序。这部分对我有用。我需要处理的是所有ajax调用完成时的处理程序。我需要做一些处理当所有呼叫结束时,即使其中一些呼叫失败。
var ajax_always_promises = [],//to be populated with promises that (barring uncaught error) are guaranteed to be resolved.
    data_arr = [],//an array to be (sparsely) populated with asynchronously delivered json data.
    error_arr = [];//an array to be (sparsely) populated with ajax error messages.

$.each(window.ids, function(i, id) {
    var dfrd = $.Deferred();
    var p = $.ajax({
        url: 'api/get_runs_stats.php',
        type: 'GET',
        dataType: 'json',
        data: {
            run_id: window.ids[i]
        }
    }).done(function(json_data) {
        data_arr[i] = json_data;//intentionally not `data_arr.push(json_data);`
    }).fail(function(jqXHR, textStatus, errorThrown) {
        error_arr[i] = textStatus;//intentionally not `error_arr.push(textStatus);`
    }).always(dfrd.resolve);

    ajax_always_promises[i] = dfrd.promise();
    doneDefers(p);
});

$.when.apply(null, ajax_always_promises).done(function() {
    //The data in the (sparsely) populated arrays `data_arr` and `error_arr` is available to be used. 
    var i, id, success_count=0, error_count=0;
    for(i=0; i<Math.max(data_arr.length,error_arr.length); i++) {

        //Here, the index i corresponds to the original index of window.ids ...
        //...that's the advantage of sparsely populating the arrays.
        id = window.ids[i];
        if(data_arr[i]) {
            //Here, do whatever is required with `data_arr[i]`, and `id` if needed.
            success_count++;
        }
        else if(error_arr[i]) {
            //Here, do whatever is required with `error_arr[i]`, and `id` if needed.
            error_count++;
        }
    }
    console.log("Success:errors: " + success_count + ':' + error_count);
});