XMLHttpRequest to MongoDB's simple rest interface

606 views
Skip to first unread message

Irfan Khan

unread,
Jan 1, 2014, 1:34:13 PM1/1/14
to mongod...@googlegroups.com
Hello,

I am trying to fetch data from Mongodb, using javascript inside html (running on apache localhost), but it is not working, request.status is never 200, it is always 0.  Otherwise, on Chrome is says : XMLHttpRequest cannot load http://localhost:28017/newdb/newcollection/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

although mozilla/opera don't give this error, but in any case  request.status is never 200, and request.responseText is always empty.

whereas, when I just  browse this address from the browser, http://localhost:28017/newdb/newcollection/    it displays the  collection,  but not from xmlhttprequest.


 here is the javascript code:
 

<script>
var request = new XMLHttpRequest();
request.open("GET", "http://localhost:28017/newdb/newcollection/", true);
request.withCredentials="true";
request.onreadystatechange = handleReadyStateChange;
request.send();
function handleReadyStateChange() {    
           if (request.readyState == 4) {  
                if (request.status == 200) { 
document.write(request.responseText);
                 }
           }
        }

</script>


Thanks a lot for any kind of suggestion.

Cheers

Irfan

Alan Spencer

unread,
Jan 6, 2014, 9:58:20 AM1/6/14
to mongod...@googlegroups.com
Hi,
For the issue you mention in Chrome, you may want to look at the answer in:
http://stackoverflow.com/questions/9310112/why-am-i-seeing-an-origin-is-not-allowed-by-access-control-allow-origin-error

Are you saying that the non 200 response is causing mozilla/opera to give an error?

If you need robust REST, you may want to use a rest interface on top of mongodb

Simple REST Interface

The mongod process includes a simple REST interface, with no support for insert/update/remove operations, as a convenience – it is generally used for monitoring/alerting scripts or administrative tasks. For full REST capabilities we recommend using an external tool such as Sleepy.Mongoose.


Thanks,
Alan.
Reply all
Reply to author
Forward
0 new messages