d3如何在邮件请求中正确传递请求正文?

时间:2017-03-22 23:47:18

标签: javascript json post d3.js

我有这个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执行此发布请求时,这是预期传递输入参数的方式: example request body example request/response

但是,当我运行此脚本时,我收到了400错误代码。我想我知道我错过了哪些可能会通过请求正文?虽然不是100%肯定。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:3)

看着你的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) {

    ...

答案 1 :(得分:0)

尝试

const MAXAGE = 3600; // seconds until recheck
const URL = `/sw.js?q=${Math.floor(Date.now() / (MAXAGE * 1000))}`;
navigator.serviceWorker.register(URL);

如果你期待JSON响应,你可以改用d3.json。