-2

I'm relatively new to javascript, and I have a restful api I'm connecting to that returns me a json string, which I can parse properly like so:

$.ajax({ url: './php/bandwidth.php',
             data: { prop_id : "1" },
             type: 'post',
             success: function(output) {

                    var json = $.parseJSON(output);


                    for( var i = 0 ; i < json.response.length; i++ ){
                        times.push  (json.response[i].time);
                    }
                }
    });

Inside of the success callback the variables in the array exist. I also have times array instantiated outside the ajax call function. But outside of the ajax call the array is empty. I'm sure it's a scoping issue. Can anyone give me a way to get the data from inside the array? Does the construct $.ajax({url:... , data:... , success: function(){}}); returns callback return value?

1 Answers1

1
$.ajax({ url: './php/bandwidth.php',
         data: { prop_id : "1" },
         type: 'post',
         dataType: 'json',
         success: function(output) {    
                times = [];                
                for( var i = 0 ; i < output.response.length; i++ ){
                    times.push  (output.response[i].time);
                }
            },
         complete: function(){
            if(times.length > 0){ console.log(times); } else { console.log("times empty"); }
         }
});
davethecoder
  • 3,856
  • 4
  • 35
  • 66