Having recently started with JavaScript, I am looking to parse JSON from an external URL using pure JavaScript. Currently, I have the following code:
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
function statsget() {
var uname = document.getElementById("nameget").value;
var data = getJSON(`https://www.reddit.com/user/${uname}/circle.json`);
var stats = JSON.parse(data);
alert(data.is_betrayed);
}
However, this implementation is not functioning as expected. Is there anyone who could assist me in resolving this issue? Thank you!