send query string via queue.defer

394 Views Asked by At

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)

1

There are 1 best solutions below

2
Kordi On BEST ANSWER

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('&');