Added GET response header. Please see if u get any clue... Thanks for your time
*Revised question with new code and the results * I have a set of json files web hosted by mongoose in my localhost. I want to read one of those json files,parse it and display in a HTML page as a table. This HTML based table is to be later consumed by another application.
Please note that the mongoose web server is just hosting the files. I am not sure if it will support any GET/POST requests.
Trying to read the file thru Ajax request. Here is my code with the console output below. Please advise.
<html>
<head>
<meta charset="UTF-8">
<title>ajaxtester</title>
<script type="text/javascript">
function loadXMLDoc(){ var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function()
{ console.log("within function");
if (xhr.readyState==4 && xhr.status==200) {
console.log("If check passed")
console.log(xhr.readyState);
console.log(xhr.status);
document.getElementById("myDiv").innerHTML=xhr.responseText;
}
else
{
console.log("Failed If check")
console.log(xhr.readyState);
console.log(xhr.status);
}
}
console.log("After rdystate function")
console.log(xhr.readyState);
xhr.open("GET","http://localhost:8080/iso.txt",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send();
console.log("SEND");
console.log(xhr.readyState);
console.log(xhr.status);
console.log(xhr.statusText);
}
</script>
</head>
<body>
<div id="myDiv">
<p>Let AJAX change this text!</p>
</div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
After rdystate function 0
within function Failed If check 1 0
GET http://localhost:8080/iso.txt
200 OK 2ms
SEND 1 0 (an empty string)
within function Failed If check 2 0
within function Failed If check 4 0
As you see there is a GET request with status code 200, but xhr.status always returns 0. Also the GET response header in firebug shows the content length correctly. But the actual response remains elusive.
Response header of GET request. Note the connection-close, Is this a normal behavior ? Response Headers Accept-Ranges bytes Connection close Content-Length 5 Content-Type text/plain Date Fri, 03 Jan 2014 15:29:41 GMT Etag "52c59fe6.5" Last-Modified Thu, 02 Jan 2014 17:20:38 GMT