Javascript相当于curl命令 [英] Javascript equivalent for curl command

查看:191
本文介绍了Javascript相当于curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  curl -X POSThttp://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}] 2013-08-06T11:30:00.000Z / 2020-08-07T11:40:00.000Z]}'


$ b b

但我不能创建一个等效的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 {
alert(data);
var pubResults = data;
},
错误:function(data){
alert(ERROR RESPONSE FROM DRUID SERVER: + JSON.stringify(data));
},
complete:function(data){
console.log(call completed);
}
} );
}

任何帮助将不胜感激。















您认为的选项是



1)使用 CORS 在远程服务器上设置标题跨域调用(如果您有权访问服务器)thriqon也指出了



2)使用 jsonP 格式从服务器的响应(如果你有控制如何生成响应或者它已经在jsonP格式)。



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


I am using below curl command to get the data from Druid cluster hosted on remote server, in the 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 am not able create an equivalent javascript method which will do the same thing, I am using below code to do 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 will be appreciated.

解决方案

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

Your options I believe are

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

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

3) Write a server-side proxy that 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

这篇关于Javascript相当于curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆