d3 Fetch Parameter

30 views
Skip to first unread message

stutr...@gmail.com

unread,
Dec 14, 2020, 4:27:12 AM12/14/20
to dc-js user group
Hi Folks,
I know this is more d3 but does anyone know how to pass a parameter with d3 fetch?

Normally I just use this:
d3.json("dataset.php").then(function(dataSet) {
//dc stuff here
});

But i'm using a (separate) Select to pick an item which I then want to pass to the above.
My latest attempt is:
d3.json("dataset.php", {
  method: "POST",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: "selectedItem= selectedItem  "
}).then(function(dataSet) {
//dc stuff here
});

dataset.php is :
<?php
$hostname_base = "localhost";
$username_base = "un";
$password_base = "pw";
$database_base = "db";

$connection = mysqli_connect($hostname_base,$username_base,$password_base,$database_base) or die("Error " . mysqli_error($connection));

$selectedItem = $_POST['selectedItem'];

 $sql = "SELECT * from myTable WHERE item = '".$selectedItem."' ";

$result = mysqli_query($connection, $sql) or die("Error in: " . mysqli_error($connection));

$emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    mysqli_close($connection);
?>

With ajax I would normally do this:
$.ajax({
url: 'dataset.php',
type: 'POST',
dataType: "json",
data: {
selectedItem:  selectedItem
},

Thanks all

Gordon Woodhull

unread,
Dec 14, 2020, 4:37:12 AM12/14/20
to dc.js user group
That looks about right, you might try

"Content-Type": "application/x-www-form-urlencoded"

or change the body to json.


--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/075ef6dc-2e42-49ae-b61b-e72b44e2c3acn%40googlegroups.com.

stutr...@gmail.com

unread,
Dec 15, 2020, 7:55:53 AM12/15/20
to dc-js user group
Thanks for the info Gordon.
In the end I modified in line with the method here.
For completeness this is what I ended up with (I left the iteration in place in case I needed to add other params later):

var data = {selectedItem:selectedItem};
var fd = new FormData();
for(var i in data){
   fd.append(i,data[i]);
}
d3.json("data/dataset.php", {
 method:"POST",
   body:fd,
   mode:"cors",
 }).then(function(dataSet) {
//dc stuff here
});

Gordon Woodhull

unread,
Dec 15, 2020, 8:09:31 AM12/15/20
to dc-js-us...@googlegroups.com
Thanks for following up and posting your solution!

Good point: as mentioned in the Q&A you linked, it depends on what content types your server can parse. And the body must match the specified content type.

The default content type is FormData - probably the oldest format and most supported.


On Dec 15, 2020, at 7:56 AM, stutr...@gmail.com <stutr...@gmail.com> wrote:

Thanks for the info Gordon.
Reply all
Reply to author
Forward
0 new messages