After getting involved with AJAX and utilizing the MAMP web server, I decided to work on a simple script to test its functionality. My attempt led me to investigate using the status property of XMLHttpRequest(); As it turns out, the issue I encountered was a 404 error (Page not found), which surprised me since I'm leveraging a JSON file that resides in the same directory as my AJAX script.
Below is the code snippet for the AJAX script:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status === 200) {
alert("Good");
}
else if(xhr.status === 304)
{
alert("304");
}
else if(xhr.status === 404)
{
alert("404");
}
else
{
alert("500");
}
xhr.open("GET", 'json.json', true);
xhr.send(null);
Additionally, here is the content of my json.json file:
{
"events" :
[
{
"location" : "San Francisco, CA"
}
]
}
Encountering this challenge has been quite frustrating. Any insights or solutions would be greatly appreciated.
Raul Rao