简体   繁体   English

使用JavaScript发送HTTP请求以从MongoDB获取结果

[英]Sending HTTP request in JavaScript to get results from MongoDB

I am running MongoDB in the REST mode and trying to get query results from the MongoDB server through HTTP request. 我在REST模式下运行MongoDB,并尝试通过HTTP请求从MongoDB服务器获取查询结果。 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: 而当我将URL复制并粘贴到浏览器的地址栏中时,会得到以下响应:

{
  "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. 有人可以帮忙告诉我可能是什么问题。

Is the page on which your code is running also loaded from 127.0.0.1 port 28017? 是否还从127.0.0.1端口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 从根本上讲,该代码有效: http : //jsbin.com/awose3


Off-topic : I'd strongly recommend not using synchronous XHR calls (ajax requests). 离题 :我强烈建议您不要使用同步XHR调用(ajax请求)。 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. 他们在处理过程中会锁定浏览器的UI,当然,它们可能需要一两秒钟(或十秒钟)才能运行,在此期间,用户体验至少是令人不快的。 Instead, use an asynchronous call and a callback on onreadystatechange , like this (obviously, error handling and other complexities have been left out): 相反,应在onreadystatechange上使用异步调用和回调,如下所示(显然,错误处理和其他复杂性已被忽略):

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 . 离题2 :我建议您查看jQueryPrototypeYUIClosure其他任何一个库。 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. 他们将简化浏览器的怪癖,简化各种工作,并添加许多有用的功能,以便您可以专注于解决您要解决的实际问题,而不必担心Safari错误报告之类的问题(随机示例) option的默认选定值,以及Internet Explorer在事件处理程序上泄漏内存的习惯,除非您在编写代码时ch吟,燃烧香气并定期起床并旋转3次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM