Javascript 如何在 d3.json 上发布参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14970578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I post parameter on d3.json?
提问by yun
I'm using d3.json to get a dynamic data.
我正在使用 d3.json 来获取动态数据。
d3.json("/myweb/totalQtyService.do", function(json) {
drawChart(json);
});
How do I post parameter on d3.json? i.e.
如何在 d3.json 上发布参数?IE
data : { year: "2012", customer: "type1“ }
数据:{ 年份:“2012”,客户:“type1”}
Any idea pass those parameter on post? not url parameter /myweb/totalQtyService.do?year=2012&customer=type1
任何想法在帖子上传递这些参数?不是 url 参数 /myweb/totalQtyService.do?year=2012&customer=type1
I tried such below, but couldn't make it work. because the data structure so different
我在下面尝试过,但无法使其工作。因为数据结构如此不同
d3.json => [Object, Object, Object, Object]
d3.json => [对象,对象,对象,对象]
$.ajax => {entity_name: "ACTIVA"entity_tar_val: 350entity_val: 400level: 1_proto_: Object},...
$.ajax => {entity_name: "ACTIVA"entity_tar_val: 350entity_val: 400level: 1_ proto_: Object},...
$.ajax({
type: "POST",
url: url,
// parameter here
data : {year: "2012", customer: "type1“},
success: function(json){
// console.log(json);
**drawChart(json);**
} ,
error:function (request, err, ex) {
}
});
回答by Tom P
NOTE: This answer applies to an older version of d3.json, see JoshuaTaylor's comment below.
注意:此答案适用于旧版本的 d3.json,请参阅下面的 JoshuaTaylor 评论。
NOTE for d3v5: The original answer is increasingly out of date. d3 v5uses the fetch APIand promises rather than XMLHttpRequest.
d3v5 的注意事项:原始答案越来越过时。d3 v5使用fetch API和 promises 而不是 XMLHttpRequest。
You can set the method of the http request in the second parameter of any of the d3-fetch data getting functions e.g.
您可以在任何 d3-fetch 数据获取函数的第二个参数中设置 http 请求的方法,例如
d3.csv("/path/to/file.csv", { method: 'POST' })
.then((data)=>{ /* do something with 'data' */ });
d3.jsonis a convenience method wrapping d3.xhr; it's hardwired to make GET requests.
d3.json是包装 d3.xhr 的便捷方法;发出 GET 请求是硬连线的。
If you want to POST you can use d3.xhrdirectly.
如果你想 POST 你可以直接使用d3.xhr。
Something like this:
像这样的东西:
d3.xhr(url)
.header("Content-Type", "application/json")
.post(
JSON.stringify({year: "2012", customer: "type1"}),
function(err, rawData){
var data = JSON.parse(rawData);
console.log("got response", data);
}
);
Because no callback is specified in creation of the request object it's not sent immediately. Once received the JSON response can be parsed in the standard JavaScript way i.e. JSON.parse(data)The documentation for d3.xhr is here.
因为在创建请求对象时没有指定回调,所以它不会立即发送。一旦接收到 JSON 响应,就可以用标准的 JavaScript 方式进行解析,即JSON.parse(data)d3.xhr 的文档在这里。
回答by FDaumas
You could also try this:
你也可以试试这个:
d3.json(url,function(error, data) {
...
})
.header("Content-Type","application/json")
.send("POST", JSON.stringify({year: "2012", customer: "type1"}));
回答by Gordon
Using d3-fetch in D3v5, it looks like
在 D3v5 中使用 d3-fetch,看起来像
d3.json(url, {
method: 'POST',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify(query)
});
Where urlis the URL and queryis the content to post.
urlURL在哪里,query是要发布的内容。
I found the answer in this question.
我在这个问题中找到了答案。
回答by juacala
If you want to make a request like an HTML form (using query parameters), then you would do:
如果你想像 HTML 表单一样发出请求(使用查询参数),那么你可以这样做:
d3.request("/path/to/resource")
.header("X-Requested-With", "XMLHttpRequest")
.header("Content-Type", "application/x-www-form-urlencoded")
.post("a=2&b=3", callback);
See the docs here: d3-request docs
请参阅此处的文档:d3-request docs
If you want to convert a simple object to a query parameter string, then you can use the following function:
如果要将简单对象转换为查询参数字符串,则可以使用以下函数:
function obj2Params(params){
var str = "";
var amp = "";
for(var p in params){
if(params.hasOwnProperty(p)) {
str += amp + encodeURIComponent(p) + "=" + encodeURIComponent(params[p]);
amp = "&";
}
}
return str;
}
回答by saman moshafi
If you use struts you can define the url and the parameter:
如果使用 struts,则可以定义 url 和参数:
<s:url action="jsondatatreeaction" var="jsonsearchTag"
namespace="/data">
<s:param name="searchType" value="%{searchType}"></s:param>
</s:url>
. . . d3.json('${jsonsearchTag}', function(error,flare) { ... } . . .

