我使用mongoDB的以下输入执行curl命令。
curl --data 'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}' 'http://72.123.xxx.xxx:27080/weather/_cmd'我想使用jsonp或json执行并获得响应。我在jsonp请求下尝试过。
var url = "http://72.123.xxx.xxx:27080/weather/_cmd";
$.getJSON(url + "?callback=?",
{
cmd:{"geoNear" : "items", "near": 6.8590845,79.9800719]}
},
function(tweets) { }
);我从控制台上什么也没得到。请帮我弄一下这个。谢谢。
发布于 2013-08-20 18:49:11
最后,我提出了解决方案。
$.ajax({
url: '/weather/_cmd',
type:'POST',
dataType: 'json',
crossDomain : true,
data: {
cmd: JSON.stringify({
"geoNear" : "items",
"near": [6.8590845,79.9800719]
})
},
success: function(res) {
//do stuff with res
}
});
ProxyPass /weather http://72.123.xxx.xxx:27080/weather/_cmd
ProxyPassReverse /weather http://72.123.xxx.xxx:27080/weather/_cmd将上面的代理传递添加到您的Apache中,并尝试以下操作。看起来不错。问题是你不能使用jsonp来传递POST请求。mongo我们需要POST请求。这是一种方法。:D我对它进行了测试,对我来说效果很好。它不接受json,您必须将其作为字符串传递。
发布于 2013-08-09 14:37:29
您可能什么也得不到,因为您的MongoDB实例不支持JSONP (并且您需要它,因为通常您不能进行跨域ajax请求)或者您的查询是不正确的(显然您必须使用?jsonp=而不是?callback=)。
一种方法是直接使用JSONP。由于您使用的是jQuery,因此可以尝试这样的操作:
$.ajax({
url: 'http://72.123.xxx.xxx:27080/weather/_cmd',
dataType: 'jsonp',
jsonp: 'jsonp', // <-- the name used in '?jsonp=' part of url
data: {
cmd: {
"geoNear" : "items",
"near": [6.8590845,79.9800719]
}
},
success: function(res) {
console.log(res);
}
});根据StackOverflow的回答:
Does MongoDB have a native REST interface?
如果您使用--jsonp选项启动MongoDB实例(为了启用对JSONP支持),它应该可以工作。不过我还没试过。
此外,还可能存在其他问题。例如,数据库可能会简单地删除连接,您可能没有权限,等等。通常情况下,从客户端直接连接到数据库从来都不是一个好主意。你应该以中间人的身份使用web服务器。
https://stackoverflow.com/questions/18141068
复制相似问题