I'm receiving through an AJAX GET request bytes series corresponding to an img. My ultimate goal is to convert these bytes into an image and to display it on the webpage (without refreshing). All the AJAX part is working perfectly, I'm receiving the data and had verified it through console log printing. But, when I'm trying to generate an html element with the raw data (in bytes, not the img yet), I does not work.
<script>
$(document).ready(function(){
setInterval(function(){
$.ajax({
type: 'GET',
url : "/checkview",
success: function(response){
//console.log(response)
let textC = response
document.body.onload = addElement;
function addElement() {
// create a new div element
const newDiv = document.createElement("div");
// and give it some content
const newContent = document.createTextNode(textC);
// add the text node to the newly created div
newDiv.appendChild(newContent);
// add the newly created element and its content into the DOM
const currentDiv = document.getElementById("div1");
document.body.insertBefore(newDiv, currentDiv);
},
error: function(response){
alert('An error occured')
}
});
},10000);
})
}
</script>
Hints: the data are supposed to be in the textC variable.
So, I tried first to:
alert(textC);
which worked perfectly. I got data in bytes displayed. But, it works only when written in the setInterval function. I tried this same command in the console, this error appeared:
VM288:1 Uncaught ReferenceError: textC is not defined
at <anonymous>:1:7
Therefore, I suppose there is a mistake in my code. Please help !