我创建了一个数组,并在每个索引中放入了其他数组。
但问题是,当我做的时候:
的console.log(COORDS [0]);
通过我:未定义
如果我做:
的console.log(coords)使用;
告诉我:
代码:
var coords = [];
$.ajax({
type: 'GET',
url: 'http://nominatim.openstreetmap.org/reverse?format=json&osm_type=R&osm_id=2532299&polygon_geojson=1',
data: { get_param: 'value' },
success: function (data) {
$.each(data.geojson.coordinates[0], function( index, value ) {
if(typeof value[0] !== 'undefined' && typeof value[1] !== 'undefined') {
coords.push([value[0], value[1]]);
}
});
}
});
console.log(coords);
console.log(coords[0]);
答案 0 :(得分:0)
发生这种情况是因为当数组到达console.log(coords[0]);
时尚未填充信息,您必须记住异步功能.ajax()
如果你想确保该功能已经完成以启动任何其他过程,你可以在最后使用.done(),如下所示:
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
上查看