0

Im trying to store API return into a var, but it´s returning undefined when trying to log it to console, it seems to be empty.

This is the code

    fetchMovies: function(city) {
        var myjson;
        $.getJSON('api/city?city=' + city, function(json) {
            // console.debug(json);
            myjson = json;
        });
        console.log(myjson); //
        this.filmer.push(this.myjson); // Push results to div?
}

console.debug(json) returns:

data: {91: "Nothing", 92: "Mr robot"}
name: "Stockholm"

End when access by browser the return looks like:

{
"name": "Stockholm",
"data": {
"91": "Nothing",
"92": "Mr Robot"
}
}

And the API request is http://localhost:8000/api/city?city=Stockholm

How do i store the JSON response?

Adam
  • 1,231
  • 1
  • 13
  • 37
  • Because `getJSON` is an async call - and `myjson` hasn't gotten the response yet. Move your `push` into the callback (and then `this` won't be the same context either) – tymeJV Oct 05 '16 at 19:01
  • [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) –  Oct 05 '16 at 19:02

0 Answers0