Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:55:34+00:00 2026-05-20T01:55:34+00:00

I am running MongoDB in the REST mode and trying to get query results

  • 0

I am running MongoDB in the REST mode and trying to get query results from the MongoDB server through HTTP request. The following is the simple code that I have written:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <script type="text/javascript">
    function httpGet(theUrl){
        //document.write(theUrl);
        var xmlHttp = null;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", theUrl, false);
        xmlHttp.send(null);
        document.getElementById("response").innerHTML=xmlHttp.responseText;
    }
    </script>
    <title>Connection</title>
</head>
<body>
    <button onclick="httpGet('http://127.0.0.1:28017/test/first/?limit=-1')" >
        Click
    </button>
    <p id="response"> </p>
</body>
</html>

But I am unable to get the response. Whereas when I copy and paste the URL in the address bar of the browser I get following as the response:

{
  "offset" : 0,
  "rows": [
       { "_id" : { "$oid" : "4d510086ce29000000007d5a" }, "date" : { "$date":60968917800000 } }
    ],

 "total_rows" : 1 ,
 "query" : {} ,
 "millis" : 0
}

Can someone help and tell me what could be the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:55:35+00:00Added an answer on May 20, 2026 at 1:55 am

    Is the page on which your code is running also loaded from 127.0.0.1 port 28017? If not, that’s your problem, you’re running into the Same Origin Policy.

    If it is (both server and port are the same), I’d recommend using a relative URL, so your

    httpGet('http://127.0.0.1:28017/test/first/?limit=-1')
    

    becomes

    httpGet('/test/first/?limit=-1')
    

    Fundamentally, the code works: http://jsbin.com/awose3


    Off-topic: I’d strongly recommend not using synchronous XHR calls (ajax requests). They lock up the UI of the browser while they’re in process, and of course they can take a second or two (or ten) to run, during which the user experience is unpleasant to say the least. Instead, use an asynchronous call and a callback on onreadystatechange, like this (obviously, error handling and other complexities have been left out):

    function httpGet(theUrl){
        //document.write(theUrl);
        var xmlHttp = null;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", theUrl, true);
        xmlHttp.onreadystatechange = handleReadyStateChange;
        xmlHttp.send(null);
    
        function handleReadyStateChange() {
          if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
              document.getElementById("response").innerHTML=xmlHttp.responseText;
            }
          }
        }
    }
    

    Live example


    Off-topic 2: I’d suggest looking at a library like jQuery, Prototype, YUI, Closure, or any of several others. They’ll smooth over browser quirks, simplify various things, and add a number of useful features so that you can concentrate on solving the actual problem you’re trying to solve, rather than worrying about things like (random example) Safari mis-reporting the default selected value of an option, and Internet Explorer’s habit of leaking memory on event handlers unless you chant, burn incense, and periodically get up and twirl three times as you write your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got mongodb running in following configuration: 1 config server, 2 mongos, 8 shards
I need to connect to a MongoDB instance from my EJB3 application, running on
Running Solr on Tomcat 7 on Win 2008 Server. I am looping through a
Running my script through Devel::NYTProf showed that the following portion of code took up
I have a Mongo server running on an Ubuntu box, and I am trying
I'm running a simple python script sending data to a mongodb #!/usr/bin/env python import
I have a MongoDB server running on an 64-bit Amazon EC2 instance (journaling enabled).
I'm trying to set up mongodb on webfaction. It works fine, currently running on
Am running MongoDB 2.2 on Ubuntu and if I run: sudo mongod I get
I'm running NodeJS and MongoDB on a linux cloud server. I need to give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.