与 curl 命令等效的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18472586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:03:16  来源:igfitidea点击:

Javascript equivalent for curl command

javascriptcurl

提问by amol-jore

I am using below curl command to get the data from Druid cluster hosted on remote server, in the json format

我使用下面的 curl 命令从远程服务器上托管的 Druid 集群中获取数据,格式为 json

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 am not able create an equivalent javascript method which will do the same thing, I am using below code to do this.

但我无法创建一个等效的 javascript 方法来做同样的事情,我使用下面的代码来做到这一点。

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 will be appreciated.

任何帮助将不胜感激。

回答by PJR

cURL is able to send and receive data cross domain to remote servers but that is not the case with javascript for security reasons

cURL 能够跨域向远程服务器发送和接收数据,但出于安全原因,javascript 并非如此

Your options I believe are

我相信你的选择是

1) Use CORSto set headers on the remote server to accept cross domain calls (if you have access to the Server) as also pointed by thriqon

1) 用于CORS在远程服务器上设置标头以接受跨域调用(如果您有权访问服务器),这也由 thriqon 指出

2) Use jsonPformat response from the server(again if you have control over how the response is generated or if its already in jsonP format)

2)使用jsonP来自服务器的格式响应(同样,如果您可以控制响应的生成方式或者它是否已经是 jsonP 格式)

3) Write a server-side proxythat acts as the intermediary between your calls and the remote server. You can then format the header or response on the proxy to enable it to respond to cross domain calls

3)编写一个server-side proxy充当您的调用和远程服务器之间的中介。然后,您可以格式化代理上的标头或响应,以使其能够响应跨域调用