对mongoDB的REST AJAX请求 [英] REST AJAX request to mongoDB

查看:54
本文介绍了对mongoDB的REST AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将XMLHttpRequest触发到mongoDB以便通过AJAX检索文档.

这是我的代码:

function getJsonDocumentByModeldid(model_id) {
    var valoreInput = document.getElementById('inputModelId').value;
    alert(valoreInput);

    $.ajax({
      url: "http://localhost:28017/test/",
      type: "get",
      //data: "filter_a=" + valoreInput,
      dataType: 'jsonp',
      crossDomain: true,

      success: function (data) {
        alert("success");
        //var json2javascript = $.parseJSON(data);
        manageLayout();
      },

      error: function (XMLHttpRequest, textStatus, errorThrown) {
       alert("Status: " + textStatus + "    Error:" + errorThrown);
      }
  });
}

我的函数总是返回错误.那是什么问题呢?

解决方案

此功能作为的一部分受支持简单(只读)REST接口,但要进行跨域请求,请 --jsonp ,否则您将受到相同来源政策问题的困扰,因为您使用的IP地址和端口发出的请求与运行mongoDB的IP地址和端口不匹配.

mongod.exe --rest --jsonp(加上您可能拥有的其他任何选项)开始 mongoDB .

以下示例页面可以通过网络服务器提供服务(例如 Apache HTTP Server ),也可以直接保存在本地并加载到浏览器作为文件.该请求用于获取有关名为 andyb 的dbCollection的信息,我首先在mongoDB中使用以下命令创建了该数据库:

db.createCollection('andyb');

HTML

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>mongoDB AJAX demo</title>
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
  <script type='text/javascript'>//<![CDATA[
  $(function(){
    $.ajax({
      url: 'http://localhost:28017/local/andyb',
      type: 'get',
      dataType: 'jsonp',
      jsonp: 'jsonp', // mongod is expecting the parameter name to be called "jsonp"
      success: function (data) {
        console.log('success', data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('error', errorThrown);
      }
    });
  });//]]>
  </script>
</head>
<body>
</body>
</html>

许多浏览器现在支持 CORS ,这是促进跨域资源的另一种(更现代的)方式. /p>

I am trying to fire a XMLHttpRequest to mongoDB to retrieve a document via AJAX.

This is my code:

function getJsonDocumentByModeldid(model_id) {
    var valoreInput = document.getElementById('inputModelId').value;
    alert(valoreInput);

    $.ajax({
      url: "http://localhost:28017/test/",
      type: "get",
      //data: "filter_a=" + valoreInput,
      dataType: 'jsonp',
      crossDomain: true,

      success: function (data) {
        alert("success");
        //var json2javascript = $.parseJSON(data);
        manageLayout();
      },

      error: function (XMLHttpRequest, textStatus, errorThrown) {
       alert("Status: " + textStatus + "    Error:" + errorThrown);
      }
  });
}

My function always returns an error. So what is the problem?

解决方案

This functionality is supported as part of the Simple (read-only) REST Interface but to make cross domain requests the --jsonp otherwise you will be subject to the Same origin policy problem, since the IP address and port that you are making the request from do not match the IP address and port that mongoDB is running on.

Start mongoDB with mongod.exe --rest --jsonp (plus any other options you may have).

The following example page can be served via a web sever (for example Apache HTTP Server) or simply saved locally and loaded in the browser as a file. The request is for information about a dbCollection called andyb, which I created in mongoDB first with:

db.createCollection('andyb');

HTML

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>mongoDB AJAX demo</title>
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
  <script type='text/javascript'>//<![CDATA[
  $(function(){
    $.ajax({
      url: 'http://localhost:28017/local/andyb',
      type: 'get',
      dataType: 'jsonp',
      jsonp: 'jsonp', // mongod is expecting the parameter name to be called "jsonp"
      success: function (data) {
        console.log('success', data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('error', errorThrown);
      }
    });
  });//]]>
  </script>
</head>
<body>
</body>
</html>

Many browsers support CORS now which is an alternative (more modern) way to facilitate cross domain resources.

这篇关于对mongoDB的REST AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆