I'm having an issue where the following code is not displaying any output. Can someone help me identify what mistake I might be making?
This is the HTML file:
<head>
<script type = "text/javascript">
function ajax_get_json() {
var hr = new XMLHTTPRequest();
hr.open("GET", "mylist.json", true);
hr.setRequestHeader("Content-type: application/json", true);
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
var results=document.getElementById("results");
results.innerHTML = data.user;
}
}
hr.send(null);
results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script type = "text/javascript">ajax_get_json();</script>
</body>
And here is the JSON file content: { "user":"John", "age":22, "country":"US" }