javascript - JSON file not loading into variable properly -


i'm trying load json file javascript file.

i did lot of research on trying understand should doing , how should work, can't load variable.

this contents of json file:

[   {     "id": 0,     "name": "valley of trials",     "desc": "hello world",     "choices": []   },   {     "id": 1,     "name": "valley of trials",     "desc": "",     "choices": []   } ] 

i'm loading , calling this:

var jsonlocations; $.getjson("../html/data.json", callbackfuncwithdata);  function callbackfuncwithdata(data) {     jsonlocations = data; }  console.log(jsonlocations[0].name); 

i read in question on site needed loaded way otherwise variable wouldn't assigned data due asynchronous calling. however, on opening file in firefox (with firebug), error saying: typeerror: jsonlocations undefined.

however, in watch list in firebug gives me rundown of variables, , can open jsonlocations , show me tree hierarchy array, objects , properties.

so why i'm getting error? surely if firebug showing jsonlocations has loaded data should displayed in console.log statement?

please let me know wrong code.

thanks, joeb.

in code, line console.log(jsonlocations[0].name); executed after $.getjson fired. not wait until ajax request complete.

to ensure log after data fetched, need move inside callback function:

var jsonlocations; $.getjson("../html/data.json", function callbackfuncwithdata(data) {     jsonlocations = data;     console.log(jsonlocations[0].name); }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -