使用 d3.json()/d3.xhr() 进行多部分发布请求

2024-05-02

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

我了解如何使用 d3.json().post() 执行 POST,如上所述here https://stackoverflow.com/questions/12493758/xhr-post-request-using-d3,但我想使用 POST 通过 multipart/form-data 将参数提交到 API。

奇怪的是,我找不到任何关于如何最好地做到这一点的资源;我最接近的是https://github.com/mbostock/d3/issues/929 https://github.com/mbostock/d3/issues/929 and https://github.com/mbostock/d3/wiki/Requests https://github.com/mbostock/d3/wiki/Requests但这些并没有真正涵盖多部分表单。

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


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

  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));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 d3.json()/d3.xhr() 进行多部分发布请求 的相关文章

随机推荐