d3.json()/ d3.xhr()的多部分发布请求

hus*_*eet 5 json d3.js

目前是否不支持使用请求提交多部分表单数据?

我知道如何执行与d3.json一个POST(),与下述后()在这里,但我想用POST通过的multipart/form-data的将参数提交给一个API.

似乎很奇怪,我找不到任何有关如何做到这一点的资源; 我来最接近的是https://github.com/mbostock/d3/issues/929https://github.com/mbostock/d3/wiki/Requests,但这些并没有真正涵盖多形式.

#929中描述的功能是否存在未记录的部分,我在d3.v3.js中找不到允许使用多部分表单的功能?有人正在研究或对此问题感兴趣吗?

Gor*_*don 3

成功的多部分帖子需要三个步骤。

  1. 添加标题Content-type: application/x-www-form-urlencoded
  2. 对表单数据进行编码
  3. 将其连接起来,就像在 URL 中指定查询字符串一样

然后将其作为 POST 数据发送。

这些都不是 d3 特有的,但我想我应该给出我的答案和一些示例代码,因为我来到这里。

示例代码:

var xhr = d3.xhr(post_url)
    .header("Content-type", "application/x-www-form-urlencoded");

xhr.post("arg1=" + encodeURIComponent(arg1) + "&arg2=" + encodeURIComponent(arg2),
  function(error, result) {
    if(error)
        throw new Error(error);
    read_paths.data(JSON.parse(result.responseText));
});
Run Code Online (Sandbox Code Playgroud)