0

I am trying to load a object using an jquery ajax call. I have a global variable called videos which I am trying to update. The ajax call returns a valid json object, but how to I copy the result to the global variable.

var videos = null; //trying to load using ajax
load_vids();
console.log(videos); //outputting null

function load_vids() {
    $.ajax({
        url: "ajax.php",
        cache: false,
        type: "POST",
        dataType: "json",
        success: function(data) {
            ajaxCallBack(data); //loads json object
        }
    });  
}
function ajaxCallBack(data){
    //wont update videos
    videos = data;
}

Thanks in advance.

hawx
  • 1,629
  • 5
  • 21
  • 37
  • 4
    *"how to I copy the result to the global variable"* Exactly how you did it. The problem is that you are accessing `videos` before the Ajax callback is executed, i.e. `videos = data;` hasn't happened yet. Ajax is **asynchronous**. – Felix Kling Oct 01 '14 at 22:16
  • Move `console.log(videos)` to the end of your ajaxCallBack function. Does that make a difference? – Rick Hitchcock Oct 01 '14 at 22:16

0 Answers0