【发布时间】:2017-05-17 09:29:40
【问题描述】:
我想通过 $.getJSON 调用访问数据,并将其作为数组返回。我的数据已正确传输到我的回调函数,但在我调用函数时仍未定义。我哪里弄错了?谁能给我看一个使用 $.getJSON 的例子吗?
我的代码:
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 :(
【问题讨论】:
标签: javascript json callback