2013-04-23 70 views
6

我试图发动一个XMLHttpRequest mongoDB通过AJAX检索文档。REST AJAX请求mongoDB

这是我的代码:

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); 
     } 
    }); 
} 

我的函数总是返回一个错误。那么问题是什么?

+1

什么错误? – andyb 2013-04-23 08:35:58

+0

ajax请求与脚本失败,而不是如果我在浏览器中复制url,服务器响应成功。 我不知道是什么问题... – ilamaiolo 2013-04-23 08:52:48

+0

警报错误是Jqueryxxxxxx没有被调用! – ilamaiolo 2013-04-23 08:55:43

回答

6

,支持该功能为Simple (read-only) REST Interface但部分进行跨域请求--jsonp否则你会受到Same origin policy问题,因为你正在从请求不匹配的IP地址的IP地址和端口和mongoDB正在运行的端口。

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

以下示例页面可通过网络服务器(例如Apache HTTP Server)提供,或者简单地保存在本地并作为文件加载到浏览器中。该请求是关于dbCollection信息称为andyb,我在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现在这是一种替代(更现代)方式来促进跨域资源。

+0

非常感谢您的回复! – ilamaiolo 2013-04-26 16:36:48

0

以前的答案可以用延迟对象被修改(见本善导: “How do I return the response from an asynchronous call?):

<!doctype html> 
<meta charset="utf-8"> 
<title>mongoDB AJAX demo</title> 
<script src="http://code.jquery.com/jquery-latest.min.js" ></script> 
<script>  
    $(function(){ 
    // add rest = true and jsonp = true to /etc/mongodb.conf, 
    // then restart mongodb 
     $.ajax({ 
       url: "http://localhost:28017/local/startup_log/", 
       type: 'get', 
       dataType: 'jsonp', 
       jsonp: 'jsonp', // mongodb is expecting that                      
      }) 
      .done(function(data) { 
       d=JSON.stringify(data,undefined,1); 
       $("code").text(d).css("color","green"); 
      }) 
      .fail(function(request,status,error) { 
       $("code").text("get failed: "+error).css("color","red"); 
      }) 
      .always(function() { 
       console.log("finished") 
      }) 

    }); 
</script> 
<body> 
    <pre> 
    <code> 
    </code> 
    </pre> 

总之,在这两种情况下,只有当没有提供端口error:fail()处理工程的,例如:

url:"localhost/asdasdasd" 

导致一个错误处理;而

url:"localhost:28017/asdasdasd" 

导致404登录控制台,像这样:

GET http://localhost:28017/?jsonp=jQuery110207253803350031376_1383522587177&_=1383522587178 404 (OK)