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

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

问题描述

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

d3.json("/myweb/totalQtyService.do", function(json) {绘制图表(json);});

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

数据:{年份:2012",客户:type1"}

任何想法在帖子上传递这些参数?不是 url 参数/myweb/totalQtyService.do?year=2012&customer=type1

我在下面尝试过,但无法使其工作.因为数据结构太不一样了

d3.json => [对象,对象,对象,对象]

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

$.ajax({类型:POST",网址:网址,//这里的参数数据:{年份:2012",客户:type1"},成功:函数(json){//console.log(json);**绘制图表(json);**} ,错误:函数(请求,错误,前){}});

解决方案

注意:此答案适用于旧版本的 d3.json,请参阅下方 JoshuaTaylor 的评论.

d3v5 的注意事项:原始答案越来越过时.d3 v5 使用 fetch API 和 promises 而不是 XMLHttpRequest.

您可以在任何d3-fetch数据获取函数的第二个参数中设置http请求的方法,例如

d3.csv("/path/to/file.csv", { method: 'POST' }).then((data)=>{/* 用'data'做一些事情 */});

<小时>

d3.json 是包装 d3.xhr 的便捷方法;发出 GET 请求是硬连线的.

如果你想发布你可以使用 d3.xhr 直接.

像这样:

d3.xhr(url).header("Content-Type", "application/json").邮政(JSON.stringify({year: "2012", customer: "type1"}),功能(错误,原始数据){var 数据 = JSON.parse(rawData);console.log("得到响应", 数据);});

因为在创建请求对象时没有指定回调,所以它不会立即发送.一旦接收到 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.

NOTE for d3v5: The original answer is increasingly out of date. d3 v5 uses the fetch API and promises rather than XMLHttpRequest.

You can set the method of the http request in the second parameter of any of the d3-fetch data getting functions e.g.

d3.csv("/path/to/file.csv", { method: 'POST' })
    .then((data)=>{ /* do something with 'data' */ });


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天全站免登陆