Let's say I have a json file named map.json :
{
"images":{
"background": ["images/mountains.png","images/sea.png"]
}
}
What I want is for javascript to access "images/mountains.png" in map.json and use it later to retrieve the mountains.png file. I found this neat code online that I used on top of my entire code:
var xh_req = new XMLHttpRequest();
xh_req.open("GET", "map.json", false);
xh_req.send(null);
var json_object = JSON.parse(xh_req.responseText);
What this basically does is to allow javascript to access the objects in map.json by simply typing json_object.images.background[n]
. So, if I want to get the "images/sea.png" from map.json, I just type json_object.images.background[1]
and it does the job. This would've been the end if it weren't for the warning that the console kept throwing at me. It says:
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
For the next few hours, I've been attempting to solve this issue by reading forums and the XMLHttpRequest documentation, and use whatever knowledge I find to rewrite a code that does the same function as the above. Yet, I can't seem to do it. I may have missed some important points which is why I'm still not able to write the correct code. Can anyone help me with this?