嗯,我使用 JQuery 进行 Ajax Post 请求并获取数据。
Ajax 工作正常,但是:
coordinates = [];
$.post("ajax_markers.php",{time:time},function(result) { coordinates=result.split(','); alert(coordinates); }); // Alerts the Coordinates as Expected :)
但是..
$.post("ajax_markers.php",{time:time},function(result) { coordinates=result.split(','); });
alert(coordinates); // Alerts with a Blank Box :(
为什么会发生这种情况?
两者都应该使用相同的数据发出警报......因为坐标对于两者来说都是全局的!
最佳答案
在这个中:
$.post("ajax_markers.php",{time:time},function(result) { coordinates=result.split(','); });
alert(coordinates);
在帖子从服务器返回之前,您会立即执行警报。
所以我想说这个问题更多地与执行顺序有关,而不是与闭包有关。
关于Javascript:闭包问题,我猜..或者什么奇怪的事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864877/