我正试图通过AJAX向mongoDB发送XMLHttpRequest
以检索文档。
这是我的代码:
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地址和端口不匹配。
启动mongoDB与mongod.exe --rest --jsonp
(加上任何其他选项,你可能有)。
下面的示例页面可以通过web服务器(例如Apache HTTP服务器)提供,或者简单地保存在本地并作为文件加载到浏览器中。该请求是关于名为andyb的dbCollection的信息,我首先在mongoDB中创建了:
db.createCollection('andyb');