0

I am having a hard time trying to get this working. My problem is that I want to run a function after I get the values for several (variable number) of AJAX calls so I can do something with those calls. The call works when I set the async to false, but that defies the AJAX purpose. How could I possible make the calls and get all the values back to run a function only when I get the results back? Next is an example of why I am trying to do (of course in reality the number of calls and parameters will vary).

<!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>

Thanks in advance :-)

  • You should make processing of ajax result in `then()` section. Do only a call in `when()`. Like this: `$.when( $.get( url1 ), $.get( url2 ), $.get( url3 ), $.get( url4 ) ).then(function() { // here arguments variable will contain the responses })` – Gino Pane Dec 09 '15 at 21:14
  • Thanks Gino, but unfortunately the number of AJAX calls is variable hence I could not add something like what you showed me $.when( $.get( url1 ), $.get( url2 ), $.get( url3 ), $.get( url4 ) ) since the arguments to $.when will vary, that's the reason why I tried to group them together in a single function call called mulJSON(url) and process the result of all AJAX calls in the .then part. – eduardo_utrecht Dec 09 '15 at 21:23

0 Answers0