我正在努力让这项工作变得困难。我的问题是我想在获得几个(可变数量)AJAX调用的值后运行一个函数,这样我就可以对这些调用做些什么。当我将async设置为false时,调用有效,但这违反了AJAX的目的。我怎么可能进行调用并获取所有值以仅在我得到结果时才运行函数?接下来是我尝试做的一个例子(当然,实际上调用和参数的数量会有所不同)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
stockholmUrl1 = 'http://frontex.webdeveloper-tips.com/ba-simple-proxy.php?url=http%3A%2F%2Ffrontex.meteogroup.com%2Fcgi-bin%2Fmapserv%3Fmap%3D%2Fvar%2Fwww%2Fmapserver%2Ffrontex%2Ffrontex_marine.map%26TIME%3D2015-11-28T00%3A00%3A00.000Z%26SERVICE%3DWMS%26VERSION%3D1.3.0%26REQUEST%3DGetFeatureInfo%26BBOX%3D7.06283156498674103%2C-44.125%2C82.93716843501326252%2C45.125%26CRS%3Depsg%3A4326%26WIDTH%3D754%26HEIGHT%3D641%26LAYERS%3DAir_temp_area%26STYLES%3D%26FORMAT%3Dimage%2Fpng%26QUERY_LAYERS%3DAir_temp_area%26INFO_FORMAT%3Dgeojson%26I%3D598%26J%3D539';
function mulJSON(url){
var requestUrl = url;
var time;
var arr=[];
var result = (function () {
for(var i=0;i<3;i++){
$.ajax({
'async': true,
'global': false,
'dataType': "json",
'url': requestUrl,
'success': function (data) {
arr.push(data.contents.features[0].properties);
}
});
}
return arr;
})();
return result;
}
$.when( mulJSON(stockholmUrl1) ).then(function( data ) {
console.log(data);
});
});
</script>
</body>
</html>
提前致谢: - )