I've written a small util for importing JSON files and store them in a variable. But sadly I'm using a deprecated function within the XMLHTTPRequest. I've been looking all over, but can't seem to find the right answer.
Purpose: Return JSON object from imported file
Problem: I can't seem to return a proper JSON object outside the onload event
var require = function (file) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", file, false);
xmlhttp.send();
return JSON.parse(xmlhttp.responseText);
};
This is how i use it:
var config = require("file.json");
What must I do to make this work without getting the following console alert:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
Thanks in advance!
Geoffrey