首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >d3如何在post请求中正确传递请求体?

d3如何在post请求中正确传递请求体?
EN

Stack Overflow用户
提问于 2017-03-22 23:47:18
回答 2查看 2.2K关注 0票数 1

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

代码语言:javascript
复制
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错误代码。我想我知道我错过了什么,这可能是通过请求的身体?但也不能百分之百确定。任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-23 01:42:56

看看你的curl,我猜是:

代码语言:javascript
复制
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) {

    ...
票数 4
EN

Stack Overflow用户

发布于 2017-03-23 00:14:31

试一试

代码语言:javascript
复制
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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42964726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档