捕获对jquery

时间:2017-01-06 03:09:03

标签: javascript jquery ajax

我的最终问题是为什么我的回复打印到控制台,但没有写入屏幕?我按照这篇文章的指示:How do I return the response from an asynchronous call?,我按照我想的方式执行回调。

我最终试图检索数组长度(见图enter image description here

但到目前为止我的所有尝试都未定义或“[object Object]”

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

		callback(response);
   })
}

1 个答案:

答案 0 :(得分: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
}