0

I am using D3 to create some county data with their choropleth example. I am using the d3.json option vs. the jquery .ajax. I have a URL that when I use it just does not load. After some research I found that the web service requires an additional parameter:

   {'county': {'year': 2002,'stat': 'crime'}} 

So the connection to this url keeps failing on me.

I tried to add the additional required parameters for the web service as follows:

d3.json("http://url/states", function (json) {
    data = "{'county': {'year': 2002,'stat': 'crime'}}"
    //basic d3 stuff from here below
    counties.selectAll("path")
        .attr("class", quantize);
    dataType = json,
        contentType = "application/json; charset=utf-8";
});

I just cannot connect without those params in the call and I am not sure how to incorporate them.

Bertjan Broeksema
  • 1,541
  • 17
  • 28
pcproff
  • 612
  • 1
  • 8
  • 30
  • Your question is vague. The second parameter to d3.json() is a callback function. That is, it is called after the data is retrieved from the webserver with the actual data as result. So, everything in that function assumes a succeeded call to the webserver (and therefore is not a configuration for that call). Can you be more specific about what the actual problem is that you encounter? – Bertjan Broeksema Nov 01 '12 at 14:59
  • The request url fails when I try to load it. It requires {'county': `{'year': 2002,'stat': 'crime'}}` as a parameter to load the JSON I'm looking for. I hope that clears that up a bit. – pcproff Nov 01 '12 at 15:12
  • 1
    Yes, now I see what you want. So you'll need to encode this information in the url somehow (depending on what is supported by the webserver). E.g. d3.json("http://url/states?level=county&year=2002&stat=crime", function(result) { ... }). What you are trying in your question will not change the request, just how you deal with the response. – Bertjan Broeksema Nov 01 '12 at 15:28
  • This is done via a SOAP web service. Is there a way to pass the web service the parameters it is looking for besides through the URL? – pcproff Nov 01 '12 at 15:32
  • I'm just trying to change stats to these two property values with jquery U/I controls so it would be beneficial to not have to load anything through the url – pcproff Nov 01 '12 at 15:42
  • Yes, but not with d3 then I think. Here's an example using xml but I guess using json should be similar: http://stackoverflow.com/questions/124269/simplest-soap-example-using-javascript. After you get the response, you can hook in d3 for the visualization. – Bertjan Broeksema Nov 01 '12 at 15:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18925/discussion-between-bertjan-broeksema-and-pcproff) – Bertjan Broeksema Nov 01 '12 at 15:44

2 Answers2

1

It might be that single quotes aren't being counted as valid syntax. You can validate your syntax at JSONLint.com

Try double quotes instead

data = '{"county": {"year": 2002, "stat": "crime"}}'
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • Tried still fails. Great resource nonetheless I'll be sure to bookmark it. When I am generating the JSON on the backend it requires `{'year': 2002,'stat': 'crime'}}` before it spits out the JSON to me or it fails. – pcproff Nov 01 '12 at 15:16
1

Part of your problem is that you try to configure the request to the web service you want to call in the code that is used to deal with the response of the web service.

It seems that d3 doesn't give you the flexibility you need if you cannot pass the required parameters in the url (e.g. http://web.service.com/states?level=county&year=2002&stat=crime). To solve this you'll have to fall back to either raw JavaScript and construct the appropriate XMLHttpRequest such as shown here: Simplest SOAP example. Or, if you're using libraries such as jquery or dojo there might be more convenient ways to achieve the same.

Once you get the result from the request, you can use the data sent with the result (assuming that it is json) and plug in d3 to visualize.

Community
  • 1
  • 1
Bertjan Broeksema
  • 1,541
  • 17
  • 28