export class JsonLoader {
load(url: string, callback: (data: any) => void, errorCallback?: (error: any) => void) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
const jsonData = JSON.parse(xhr.responseText);
callback(jsonData);
} catch (e) {
errorCallback && errorCallback(`Failed to parse JSON: ${e}`);
}
} else {
errorCallback && errorCallback(`HTTP Error: ${xhr.status}`);
}
}
};
xhr.open("GET", url, true);
xhr.send();
}
}