Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute curl command using jquery-ajax

I executed curl command using following input to the mongoDB.

curl --data 'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}' 'http://72.123.xxx.xxx:27080/weather/_cmd'

I want to execute that using jsonp or json and get the response. I Tried below jsonp request.

var url =  "http://72.123.xxx.xxx:27080/weather/_cmd";
$.getJSON(url + "?callback=?",
    {
        cmd:{"geoNear" : "items", "near": 6.8590845,79.9800719]}
    },
    function(tweets) { }
);

I got Nothing from Console. Please help me with this. thanks.

like image 345
Lasitha Benaragama Avatar asked Aug 09 '13 06:08

Lasitha Benaragama


2 Answers

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/_cmd
ProxyPassReverse /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.

like image 132
Lasitha Benaragama Avatar answered Nov 13 '22 13:11

Lasitha Benaragama


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.

like image 35
freakish Avatar answered Nov 13 '22 13:11

freakish