Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Authorization Header while accessing JSON in D3

I am working on D3 graphs to build some Interactive Dashboard. I can able to integrate graphs using static JSON. Now I want to get JSON from my running Server. My server needs some kind of basic Authorization.

I can able to get XML Response in Restclient after adding Basic Authentication Header. but in D3 it is not working.

My D3 is looking like this ...

d3.json("http://localhost:9001/***/rest/products", function(error, root) {
   var node = svg1.selectAll(".node")
  .data(bubble.nodes(classes(root))
  .filter(function(d) { return !d.children; }))
.enter().append("g")
  .attr("class", "node")
  .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

   node.append("title")
     .text(function(d) { return d.className + ": " + format(d.value); });

   node.append("circle")
    .attr("r", function(d) { return d.r; })
    .style("fill", function(d) { return color(d.packageName); });

   node.append("text")
    .attr("dy", ".3em")
    .style("text-anchor", "middle")
    .text(function(d) { return d.className.substring(0, d.r / 3); });
 });

It will give 403 Forbidden error because Here I have not added Authentication header.

How can I add authentication header in D3.json so that I can access my resource in JSON form without any permission denied problem.

like image 703
Free-Minded Avatar asked Mar 19 '23 10:03

Free-Minded


1 Answers

You can add request headers with the #header method:

https://github.com/mbostock/d3/wiki/Requests#header

d3.json("http://localhost:9001/***/rest/products") .header("header-name", "header-value") .get(function(error, root) { // Your code here. })

like image 116
benpickles Avatar answered Mar 22 '23 23:03

benpickles