Imagine having a JSON file called map.json:
{
"images":{
"background": ["images/mountains.png","images/sea.png"]
}
}
The goal is for JavaScript to retrieve "images/mountains.png" from map.json and use it later to access the mountains.png file. I came across a helpful piece of code online that I integrated into my existing 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);
This script grants JavaScript the ability to fetch objects in map.json by simply using json_object.images.background[n]
. Therefore, retrieving "images/sea.png" can be achieved by typing json_object.images.background[1]
. However, this process was hindered by a warning from the console, stating:
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
Spending several hours attempting to resolve this issue through research and consulting forums and XMLHttpRequest documentation has been in vain. Despite my efforts, I have not been successful in rewriting the code correctly. It seems I may have overlooked crucial points, preventing me from finding the correct solution. Can someone provide assistance with this matter?