I want print data from my JSON in HTML Code
<body>
<div id="example"></div>
<div id="testing"></div>
<div id="demo"></div>
</body>
<script>
loadDoc()
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
let data= JSON.parse(this.responseText);
let example = document.querySelector('#example');
example.innerHTML = JSON.stringify(data.lose[0]["Preis"]); <---- here it works, i can change the innerHTML of "example"
}
};
xhttp.open("GET", "lose.json");
xhttp.send();
}
testing.innerHTML = JSON.stringify(data.lose[1]["Preis"]) <---- outside the function loadDoc() i dont got access to data and i cant use the JSON data.
</script>
</html>
So my Question how can i get access to my JSON data outside this loadDoc() function, because i want to use multiple entries of the imported JSON in my HTML.