$.getJSON example on callback function
I want to access data from a $.getJSON call, and return it as an array. My data is transferred correctly to my callback function, but is still undefined when I call my function. Where did I get it wrong? Can anyone please show me an example using $.getJSON?
My code:
- function countTypes( resource ) {
- $.getJSON( resource, otherFunction );
- }
- function otherFunction( data ) {
- var types = [];
- $.each(data, function( i, item ) {
- types.push( item['id'] );
- });
- console.log( types ); // This one works :)
- return types;
- }
- types = countTypes( 'my_resource' ); // types is undefined :(