1

I have a website with a form (currently it's plain HTML but we're switching to JQuery). The flow is like this :

  • Take in the user's inputs --- 5 integers
  • Make a call to a web service via REST
  • Run some calculations server side... and generate a JSON object

    ??? Now I have my result JSON object, but how do I get it to the client? Persist it? ??? Does the URL need to be the exact location of the JSON file? That would mean 100's ??? of JSON files in my web server's DIR.

  • D3.js on the client side, waits for the data to be present (via AJAX?).

    var data; // a global
    
    d3.json("path/to/file.json", function(error, json) {
    if (error) return console.warn(error);
         data = json;
         visualizeit();
    });
    
  • Once data is present, render for the client, and remove the calculator from the screen.

I'm having a tough time here, because I notice all AJAX requests need a URL.. but then does that mean I need to give a unique URL to each resulting JSON object? Doesn't that mean I need to persist that object?

I'd just like to have d3.js render my JSON object but unclear what are my options for where to put it.

Erik
  • 2,782
  • 3
  • 34
  • 64

1 Answers1

2

How do I post parameter on d3.json?

Typically, you will pass some parameters via a javascript object or a query parameter. Basically, something like...

data = {}

data.var1 =5;

data.var2 =10;

var my_request = d3.xhr(url)

my_request.post(JSON.stringify(data), function(error,received)){

};

Or

d3.json(url+"?"+"var1=5&var2=10",function(error,received)){

}

Obviously, these form parameters can be parsed on the server easily. After the values are parsed on sever, the server can generate the new JSON using the form parameters that have been parsed.

Make sure that if you are running the script from a local page (or a page that is not on the website) that the server has the ability to allow cross origin (or domain) requests.

If you want to persist the data across multiple calls, you will probably have to to embed the callbacks, or use global variables.

Community
  • 1
  • 1
jmpyle771
  • 635
  • 5
  • 15
  • So its like im performing a POST ... To a page that will return a JSON object? – Erik Feb 13 '14 at 00:09
  • Yeah, or you can do a GET. The computations shouldn't be too expensive, though. – jmpyle771 Feb 13 '14 at 00:12
  • I see you put my input data as a JSON object... My confusion was surrounding how to capture the web services response JSON.. Seems i just need to write a web service that outputs JSON and d3 will be happy – Erik Feb 13 '14 at 00:28