如何在d3.json上发布参数? [英] How do I post parameter on d3.json?

查看:996
本文介绍了如何在d3.json上发布参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用d3.json获取动态数据。

  d3.json(/ myweb / totalQtyService。 do,function(json){

drawChart(json);
});

如何在d3.json上发布参数?
ie



data:{
year:2012,
customer:type1
} >

任何想法在帖子中传递这些参数,不是url参数/myweb/totalQtyService.do??hl=zh-CN&answer=c5,b
因为数据结构如此不同



d3.json => [Object,Object,Object,Object]



$。ajax => {entity_name:ACTIVAentity_tar_val:350entity_val:400level:1_ proto _:Object},...

  $。ajax({
type:POST,
url:url,

// parameter here
data:{year:2012,customer:type1},
success:function(json){
// console.log );
** drawChart(json); **
},
错误:function(request,err,ex){
}
}


解决方案

注意:此答案适用于旧版本的d3 .json,see JoshuaTaylor's comment below。






d3.json 是一个方便的方法,包装d3.xhr;



如果您要POST,可以使用 d3.xhr



这样:

  d3.xhr(url)
.header(Content-Type,application / json)
.post(
JSON.stringify({year:2012 ,customer:type1}),
function(err,rawData){
var data = JSON.parse(rawData);
console.log(got response,data);
}
);

因为在创建请求对象时没有指定回调,所以不会立即发送。一旦收到JSON响应,就可以用标准的JavaScript方式解析。 JSON.parse(data)
d3.xhr的文档是此处


I'm using d3.json to get a dynamic data.

d3.json("/myweb/totalQtyService.do", function(json) {

    drawChart(json);
});

How do I post parameter on d3.json? i.e.

data : { year: "2012", customer: "type1" }

Any idea pass those parameter on post? not url parameter /myweb/totalQtyService.do?year=2012&customer=type1

I tried such below, but couldn't make it work. because the data structure so different

d3.json => [Object, Object, Object, Object]

$.ajax => {entity_name: "ACTIVA"entity_tar_val: 350entity_val: 400level: 1_proto_: Object},...

$.ajax({ 
  type: "POST", 
  url: url,

  // parameter here
  data : {year: "2012", customer: "type1"},
  success: function(json){
       // console.log(json);
       **drawChart(json);**
  } ,
  error:function (request, err, ex) {
  }
 });

解决方案

NOTE: This answer applies to an older version of d3.json, see JoshuaTaylor's comment below.


d3.json is a convenience method wrapping d3.xhr; it's hardwired to make GET requests.

If you want to POST you can use d3.xhr directly.

Something like this:

d3.xhr(url)
    .header("Content-Type", "application/json")
    .post(
        JSON.stringify({year: "2012", customer: "type1"}),
        function(err, rawData){
            var data = JSON.parse(rawData);
            console.log("got response", data);
        }
    );

Because no callback is specified in creation of the request object it's not sent immediately. Once received the JSON response can be parsed in the standard JavaScript way i.e. JSON.parse(data) The documentation for d3.xhr is here.

这篇关于如何在d3.json上发布参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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