我有一个ajax调用,它返回带有lat和long的数据。
$.ajax(
{
url: 'json/data.json',
dataType : 'json',
type: 'get',
cache: false,
success: function(fullJSONData)
{
$(fullJSONData.events).each(function(index, oneJSONLine)
{
$('#eventList').append(newEvent(index, oneJSONLine))
});
},
error: function (e)
{
console.log("error " + e.message);
}
});
我正在循环使用.each,这里使用newEvent函数我调用另一个ajax请求来反转地理编码lat和long。
function newEvent(iter, event)
{
var placeName
getGeoCode(event.lat, event.long, function(returnAddress)
{
placeName = returnAddress
//other processing
}
}
function getGeoCode(lat, long, callback)
{
var newurl = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
newurl += lat
newurl += ','
newurl += long
newurl += '&key=AIzaSyCgISQV9F_6VW_wqyJk9rM5rTWvoD_eqVs'
$.ajax(
{
//GeoCode Api
url: newurl,
dataType : 'json',
type: 'get',
cache: false,
success: function(fullJSONData)
{
callback(fullJSONData.results[0].formatted_address)
},
error: function (e)
{
console.log("error " + e.message);
}
});
我想我明白发生了什么,但我对如何解决它感到有点失落。我相信.each并没有等待它内部的ajax完成,因为我想象会发生,因为它是ajax。我可以强制.each等到每个内部ajax调用完成后再继续吗?我已经尝试了async:false
但是我收到了一个说明错误,说它在主线程上不好。
有人能指出我正确的方向吗?我只是让回调函数为第二个ajax调用工作,并尝试在第一个ajax调用周围放一个但是它有点超出我。
由于
答案 0 :(得分:0)
最近在chrome 52(2016年8月)中引入的一个错误也可能导致这种行为。它阻止正确处理从成功处理程序启动的ajax GET请求。 总而言之,它不会持续很长时间。
https://bugs.chromium.org/p/chromium/issues/detail?id=633696
尝试将cache:false添加到您的ajax调用