0

How do I send data (query string) using the queue defer methods?

Currently I use d3.json to get a static file as below.

queue()
    .defer(d3.json, "js/file1.json")
    .defer(d3.xhr, 'js/file2.json')
    .await(callback)

now, I need to also 'GET' a .php file, possibly sending some data via query string. In JQuery, I do

$.getJSON('ajax/file1.php', {data: some_var}, callback)

So, I tried to wrap the above in a function and pass it to defer.

get_paths = function(path) {$.getJSON(path, {data: some_var})}
queue()
    .defer(d3.json, "js/world-110m_MC.json")
    .defer(get_paths, 'ajax/file1.php')
    .await(callback);

but, unfortunately, callback is not invoke at all (though, I see the two ajax requests being made via the network tab in chrome)

Michele
  • 8,563
  • 6
  • 45
  • 72

1 Answers1

1

You could just do this if you wanna add a query string.

queue()
    .defer(d3.json, "js/world-110m_MC.json")
    .defer(d3.json, 'ajax/file1.php?data=' + $.param(some_var))
    .await(callback);

To build the query String without jquery if you have more than one parameter you could just use code like this

var params = {
    parameter1: 'value1',
    parameter2: 'value2',
    parameter3: 'value3' 
};

var queryString = Object.keys(params)
    .map(k => k + '=' + encodeURIComponent(params[k]))
    .join('&');
Kordi
  • 2,405
  • 1
  • 14
  • 13
  • my params contains nested arrays of objects, so for laziness I was looking for a way to directly pass the variable to the function (like in $.getJSON) – Michele Mar 01 '16 at 15:20
  • just build the params with jquery should be very easy, too. Think it has something todo with the callback, that jquery passes back. Just use http://api.jquery.com/jquery.param/ for the queryString. @Michele fixed my answer so its really easy, now. – Kordi Mar 01 '16 at 15:22