4

I have been hitting the same posts and API documentation for hours and I just can't seem to get this combination right. I am using D3JS v4.

I need to send a variable number of parameters to a PHP script which will return json back to me. At the moment the only way I've been able to get my parameters passed is to manually concatenate the URL together, which isn't realistic. Something like:

var id = 12345;
var type = foo;
var url = "getNetworkData.php?id="+id+"&type="+type;
d3.json(url, function (error, data) {
    if (error) throw error;                
    buildNetwork(data);
})

I have tried to use the following:

d3.request(url)
    .mimeType("application/json")
    .response(function(xhr) { return JSON.parse(xhr.responseText); })
    .get( { 'id': id , 'type':type } , function (error, data) {
        if (error) throw error;                
        buildNetwork(data);
    }
);

Or:

var url = "getNetworkData.php";    
d3.json(url, function (error, data) {
    if (error) throw error;                
    buildNetwork(data);
 })
.send("GET","id=12345");

I have also just tried passing a flat string instead of the data pairs, like "id=1234&type=foo"

But nothing seems to be actually passing to the PHP script. Can someone clear this up for me? How do I call my PHP script, give it X number of params so the script can dynamically run my queries and return the data to me in JSON?

Ognami
  • 190
  • 1
  • 8
  • Your first example is very close. Here's some advice on creating query parameters that might get you going: http://stackoverflow.com/questions/111529/how-to-create-query-parameters-in-javascript – Andrew Feb 07 '17 at 16:47
  • In D3JS I've seen various examples where the author uses data pairs to send data in their requests, similar to my second block of code. But mine doesn't ever seem to pass. If encoding to a string is necessary then I guess I will do that but would always prefer to send the pairs. – Ognami Feb 08 '17 at 14:46

0 Answers0