Javascript 在$.when.done之外从ajax添加对象

Javascript 在$.when.done之外从ajax添加对象,javascript,jquery,ajax,Javascript,Jquery,Ajax,我需要从对项的调用中获取一个新的对象{counter:count},该项是对象行的嵌套部分。我不知道如何将对象从$.when…})推送到项目中这是您所需要的: function ajaxCall1(){ return $.ajax({ url : 'URL1', data : { id : id }, type : 'GET', }); } function ajaxCall2(item_id) { return $.aja

我需要从对项的调用中获取一个新的对象
{counter:count}
,该项是对象
行的嵌套部分。我不知道如何将对象从
$.when…})推送到项目中

这是您所需要的:

function ajaxCall1(){
    return $.ajax({
        url : 'URL1',
        data : { id : id },
        type : 'GET',
    });
}
function ajaxCall2(item_id)
{
    return $.ajax({
        url: 'URL2',
        data: { item_id: item_id },
        dataType: "text",
        type: 'GET',
    });
}

$.when(ajaxCall1()).done(function(columns){
    $.each(columns, function(column, rows) {
        $.each(rows, function(i, row) {
            $.each(row.items, function(i, item) {
                $.when(ajaxCall2(item.id)).done(function(count){
                    item.counter = count;
                });
                console.log(item);
            });
        });
    });
});

您希望
$.when
停止执行直到完成,但事实并非如此。执行将在
$之后立即继续。当
完成时,仅执行回调

如果您希望在所有ajax调用完成时进行回调,可以将它们添加到数组中,并在延迟数组上使用final
$.when

ajaxCall1().done(function(columns){ // do first ajax

    var promiseArray =[]; // array for promises of subsequent ajax calls
    $.each(columns, function(column, rows) {
        $.each(rows, function(i, row) {
            $.each(row.items, function(i, item) {
                 // each call returns a promise...and also has it's own "done" to update count               
                 var promise =  ajaxCall2(item.id).done(function(count){
                    item.counter = count;
                });
                // push each promise returned from `$.ajax` into array              
                promiseArray .push(promise);                
            });
        });
    });
    // all calls will be completed, can do something with main columns array
    $.when.apply($, promiseArray).done(function(){
        /// do something with columns
    });
});

你确定不是吗?这不正是您昨天发布的异步调用的问题吗?@DaveNewton在解决异步调用时使用$来实现此方法?如果这是正确的方法,并且我在$.when…})之后执行了console.log(item);,如果没有我们推测的实际代码,该对象没有添加到Item中。您无法在
$.when()之后访问数据,并且希望它能够更新。您到底想做什么?我怀疑您需要将每个
getData
推送到一个数组中并使用
$。当
在该数组上时,它只在所有调用完成后才会触发,您需要查看
getData()
返回first though确实不需要第一个或中间的
$。当
$.ajax
有自己的
完成时
无论如何,很抱歉问这个问题,但是$.when.apply($,promiseArray.done(){真的意思吗?我在这方面出错了。美元应该是ajaxCall1吗?它使用的是
函数。apply()
将承诺数组作为参数处理。与
$类似。当(promise1,promise2….,promise10)
数组中的所有承诺都已解决之前,它不会启动。我在该特定语句中遇到错误,我不知道原因。好吧……小提示,我忽略了
函数(){
在上次
完成时
…刚刚修复了它
var deferreds = [];
$.when(ajaxCall1()).done(function(columns){
    $.each(columns, function(column, rows) {
        $.each(rows, function(i, row) {
            $.each(row.items, function(i, item) {
                var d = ajaxCall2(item.id);
                deferreds.push(d);

                $.when(d).done(function(count){
                    item.counter = count;
                });
            });
        });
    });
});

$.when.apply(null, deferreds).done(function(){
    console.log( 'All ajax calls are done and each item should have a counter property' );
});