Michael Davis
Michael Davis

Reputation: 54

Capturing asynchronous response to jquery

My ultimate question is why does my response print to console, but not write to the screen? I've followed the directions to this post: How do I return the response from an asynchronous call? and I'm executing the callback the way I THINK I should be.

I'm ultimately trying to retrieve the array length (seen in the image hereenter image description here

but all my attempts so far have been undefined or "[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);
   })
}

Upvotes: 1

Views: 52

Answers (1)

Yaser
Yaser

Reputation: 5719

Because you are sending that object and it has - in the name of the property you will need to pass it like this:

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

Of course you also can pass the whole response (if you need to check the status) and get it there:

callback(response);

And:

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

Upvotes: 1

Related Questions