I am new to both Ajax and MongoDB. I wanted to visualize some of my MongoDB data using a web browser, which is currently running on the same host. My idea was to retrieve the data through XMLHttpRequests. The MongoDB instance is running with the --rest option and when I access hxxp://localhost:28017/test_db/ss_test/ in Firefox, I receive the expected response (a JSON document containing data from the ss_test collection in the test_db database). Everything seems to be working fine so far.
I created a JavaScript function that I linked to a button's "onclick" event:
function makeRequest()
{
var myrequest = new XMLHttpRequest();
myrequest.onreadystatechange = function()
{
alert("status=" + myrequest.status + " readyState=" + myrequest.readyState)
if (myrequest.status == 200 && myrequest.readyState == 4)
{
// ...do something with the response
}
}
myrequest.open("GET", "http://localhost:28017/test_db/ss_test/", true);
myrequest.send();
}
When I load the HTML file in Firefox, open the console, and click the button, I can see that the http request is being sent, the status code is "HTTP/1.0 200 OK," and a response with Content-Length: 219257 is received, which is promising. However, the XMLHttpRequest object does not show the status=200. The alerts constantly display a status of 0 while the readyState progresses from 1, 2, to 4, and my if statement remains false.
Can someone please guide me on where I might be going wrong? Initially, I suspected issues with loading the HTML via the file protocol or potential same-origin policy problems, but even after moving the HTML file to a localhost web server and accessing it from there, the problem persists. Thank you for any help!