【问题标题】:d3 how to properly pass request body in a post request?d3 如何在发布请求中正确传递请求正文?
【发布时间】:2017-03-22 23:47:18
【问题描述】:

我有这个 d3 脚本,它根据对动态 API 的发布请求的返回 JSON 结果生成一个表。

d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search")
.header("Content-Type", "application/json")
.post("intent=data-quality", function (error,data){

                      function tabulate(data, columns) {
                            var table = d3.select('#response').append('table')
                            var thead = table.append('thead')
                            var tbody = table.append('tbody');

                            // append the header row
                            thead.append('tr')
                              .selectAll('th')
                              .data(columns).enter()
                              .append('th')
                                .text(function (column) { return column; });

                            // create a row for each object in the data
                            var rows = tbody.selectAll('tr')
                              .data(data)
                              .enter()
                              .append('tr');

                    console.log(data)
                      // create a cell in each row for each column
                      var cells = rows.selectAll('td')
                              .data(function (row) {
                                return columns.map(function (column) {
                                  return {column: column, value: row[column]};
                                });
                              })
                              .enter()
                              .append('td')
                                .text(function (d) { return d.value; });
                      return table;
                    }
                        // render the table
                        tabulate(data, d3.keys(data[0]));  

                    });

当向 API 执行这个 post 请求时,这是预期的传递输入参数的工作方式:

但是,当我运行此脚本时,我收到了 400 错误代码返回。我想我知道我缺少什么可能正在传递请求正文?虽然不是 100% 确定。任何帮助表示赞赏。

【问题讨论】:

    标签: javascript json post d3.js


    【解决方案1】:

    看看你的curl,我猜是:

    var data = {
      "action": "string",
      "fields": [
        "string"
      ],
      "filters": {}
    };
    
    d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search?intent=data-quality")
      .header("Content-Type", "application/json")
      .post(JSON.stringify(data), function (error,data) {
    
        ...
    

    【讨论】:

    • 谢谢,“.post(JSON.stringify(data)”是我需要的。
    【解决方案2】:

    试试

    d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search")
      .header("Content-Type", "application/x-www-form-urlencoded")
      .mimeType("application/json")
      .post("intent=data-quality", function (error,data){...});
    

    如果您期待 JSON 响应,您可以使用 d3.json 代替。

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 2020-05-11
      • 2012-06-24
      • 2018-08-18
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多