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)

1
  • 1
    i dont't get it. Why not just using d3.json and make the call to your php file? Commented Mar 1, 2016 at 14:53

1 Answer 1

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('&');
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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 api.jquery.com/jquery.param for the queryString. @Michele fixed my answer so its really easy, now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.