This is my JSON file:myjson.json
{
"events" : [
{ "location" : "San Francisco, CA", "date" : "May 1", "map" :"JSON/num1.jpg"},
{ "location" : "Austin , TX", "date" : "May 15", "map" :"JSON/num2.jpg"},
{ "location" : "New York , NY", "date" : "May 30", "map" :"JSON/num3.jpg"}
]
}
Here is my main file:P1.html
<!DOCTYPE html>
<html>
<body>
<h2>JSON User Account Example</h2>
<script>
var xhr = new XMLHttpRequest();
xhr.onload = function () {
console.log("xhr.status: " + xhr.status);
if (xhr.status === 200) {
responseObj = JSON.parse(xhr.responseText);
var newContent = '';
var i;
for (i = 0; i < responseObj.events.length; i++) {
newContent += '<div class="event">';
newContent += '<img src="' + responseObj.events[i].map + '"';
newContent += 'alt="' + responseObj.events[i].location + '" />';
newContent += '<p><b>' + responseObj.events[i].location + '</b><br>';
newContent += responseObj.events[i].date + '</p>';
newContent += '</div>';
}
document.getElementById("myuser").innerHTML = newContent;
}
};
xhr.open("GET", "JSON/myjson.json", true);
xhr.send(null);
</script>
<div id="myuser"></div>
</body>
</html>
When I run the code the only thing that show is the title other than that, nothing. I can't figure it out. The JSON file is located in the same folder as the html file. I don't understand why it doesn't show on the web browser.
I changed the folder name to JSON to make it simpler and applied all those changes everyone pointed out. All the files are in the same folder and server and still, it does not load the images. frustrated.
So I just checked the developer tool in the browser and this is what I got:
Access to XMLHttpRequest at 'file:///C:/Users/OneDrive/Desktop/Stack/2021/JSON/myjson.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, brave, chrome-untrusted, https.