2017-01-06 42 views
1

我的最終問題是爲什麼我的響應打印到控制檯,但不寫入屏幕?我按照這個帖子的指示:How do I return the response from an asynchronous call?,我按照我認爲應該的方式執行回調。捕獲對jQuery的異步響應

我最終試圖檢索數組長度(這裏的圖像enter image description here

但我所有的努力,到目前爲止已經未定義或「的翻譯:」

function myCallback(result) { 
 
\t \t document.write(result); //prints [object Object] 
 
\t \t console.log(result); //prints all response data to console 
 
} 
 
foo(myCallback); 
 
function foo (callback){ 
 
\t $.ajax({ 
 
\t \t url: 'https://' + company + '.teamwork.com/' + action, 
 
\t \t headers: {"Authorization": "BASIC " + window.btoa(key)}, 
 
\t \t processData: true, 
 
\t \t data: {}, 
 
\t \t dataType: 'json', 
 
\t }).done(function(response){ 
 

 
\t \t callback(response); 
 
    }) 
 
}
中所看到的

+1

只是使用'response.todo您的回調中的「-items」 – Yaser

+0

我也試過。我收到一個'Uncaught ReferenceError:items is not defined'消息。我猜連字符會把它扔掉? –

+2

試試這個:'callback(response ['todo-items'])' – Yaser

回答

1

因爲您要發送該物品,並且該物品的名稱中有-,您需要將其通過如下方式:

callback(response['todo-items']); 

當然,你也可以通過全響應(如果你需要檢查狀態),並得到它還有:

callback(response); 

和:

function myCallback(result) { 
    document.write(result['todo-items']); //prints todo items 
    console.log(result); //prints all response data to console 
}