I am trying to display an image after getting the image using ajax but it doesn’t display. I can’t seem to figure out what I did wrong. From the console.log(data.image); i can see the image but it just won’t display on the page.
URL="testurl";
function getdata() {
let xhr = new XMLHttpRequest();
let img2 = document.getElementById("image");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
var data = JSON.parse(xhr.responseText);
var image = document.createElement("img");
image.src=data.image;
img2.appendChild(image);
}
xhr.open('GET', URL);
xhr.send();
}