Execute curl command using jquery-ajax Execute curl command using jquery-ajax curl curl

Execute curl command using jquery-ajax


Finally I Come up with the Solution.

$.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/_cmdProxyPassReverse /weather http://72.123.xxx.xxx:27080/weather/_cmd

Add above Proxy pass to your Apache and try this. it will work. problem is you cant pass POST Request by using jsonp. mongo we need POST requests. this is one way to do it. :D i tested it and works perfectly for me. it will not accept json and you have to pass it as string.


You probably get nothing because either your MongoDB instance does not support JSONP (and you need it because normally you can't do cross-domain ajax requests) or your query is incorrect (apparently you have to use ?jsonp= instead of ?callback=).

One way would be to use JSONP directly. Since you are using jQuery you could try something like that:

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

According to this StackOverflow answer:

Does MongoDB have a native REST interface?

it should work if you fire MongoDB instance with --jsonp option (in order to enable support of JSONP). I haven't tried it though.

Also there might be other issues. For example the database may simply drop the connection, you might not have privileges, etc. Generally it is never a good idea to connect from client to database directly. You should use a web server as a man in the middle.