Javascript equivalent for curl command

I use the command below curl to retrieve data from a Druid cluster hosted on a remote server in json format

curl -X POST "http://www.myserverIP.com:8080/druid/v2/" -H 'content-type: application/json' -d '{"queryType": "groupBy","dataSource": "twsample","granularity": "none","dimensions": ["created_at"],"aggregations": [{"type": "count", "name": "tweetcount"}],"intervals": ["2013-08-06T11:30:00.000Z/2020-08-07T11:40:00.000Z"]}' 

but I cannot create an equivalent javascript method that will do the same, I use the code below for this.

 function sendCurlRequest(){ var json_data = { "queryType": "groupBy", "dataSource": "twsample", "granularity": "none", "dimensions": ["created_at"], "aggregations": [ {"type": "count", "name": "tweetcount"} ], "intervals": ["2013-08-06T11:30:00.000Z/2020-08-07T11:40:00.000Z"] }; $.ajax({ cache : false, type: 'POST', crossDomain:true, url: 'http://www.myserverIP:8080/druid/v2/', data:json_data, //dataType: "jsonp", contentType:"application/jsonp", success: function(data){ alert(data); var pubResults = data; }, error: function(data){ alert("ERROR RESPONSE FROM DRUID SERVER : "+JSON.stringify(data)); }, complete: function(data){ console.log("call completed"); } }); } 

Any help would be appreciated.

+7
javascript curl
source share
1 answer

cURL can send and receive cross-domain data to remote servers, but this does not apply to javascript for security reasons.

Your options, I believe

1) Use CORS to set headers on the remote server to receive cross domain calls (if you have access to the Server), also specified by thriqon

2) Use a jsonP response on the server (again, if you have control over how the response is generated or if it is already in jsonP format)

3) Write a server-side proxy that acts as an intermediary between your calls and the remote server. You can then format the header or response to the proxy so that it can answer cross-domain calls

+4
source share

All Articles