1

Background: I'm attempting to use d3 to create a choropleth as demonstrated here: http://bl.ocks.org/mbostock/4060606

My data is coming from a json-rpc api.

var rpcUrl = "http://localhost:3001/rpc/json";

var rpcData = '{"params": {}, "jsonrpc": "2.0", "method": "foo.method", "id": "bar"}';

console.log(d3.xhr(rpcUrl).post(rpcData, function(error, data){
    console.log(error);
    console.log(data);
}));

I'm getting the responseText:

responseText: "{"jsonrpc":"2.0","error":{"code":"bad_request","message":"JSON RPC requires a 'method' field"}}"

My method is obviously there in rpcData, so it appears I'm not actually providing the object at all. I suspect my trouble is misunderstanding of how d3.xhr is called. Any clues?

Note: if you are looking at the background info, you'll note that d3.xhr(url).post() is intended to be called using queue.js. I'm providing the simplified call above to avoid any confusion as to the source of the problem.

lysdexia
  • 1,786
  • 18
  • 29
  • Did you verify that your post data is correct? If you can issue a GET request with the parameters in the URL it would be much easier because you would be able to use `d3.json` directly. – Lars Kotthoff Oct 31 '13 at 09:59
  • Yep, POST data is correct. I've tested it using both the postman browser tool for chrome and a simple python script. The json-rpc end-point I'm connecting to requires POST. I don't think I've ever run onto a json-rpc implementation that uses GET, but hey, I am only an egg. – lysdexia Oct 31 '13 at 13:31
  • Have you tried sending `rpcData` as an object and not as a string (i.e. without the quote marks around it)? – Lars Kotthoff Oct 31 '13 at 13:43
  • Yes. I tried that first. The api docs mention no magical json conversion, but I was hoping. :-) – lysdexia Oct 31 '13 at 15:09

1 Answers1

2

I ran into the same problem of d3.xhr.post seemingly not sending any post data. Setting the content-type header information to application/json fixed it for me. Here is your updated example:

var rpcUrl = "http://localhost:3001/rpc/json";
var rpcData = '{"params": {}, "jsonrpc": "2.0", "method": "foo.method", "id": "bar"}';
console.log(d3.xhr(rpcUrl)
  .header("Content-Type", "application/json")
  .post(rpcData, function(error, data) {
     console.log(error);
     console.log(data);
   })
);
erik_w
  • 66
  • 5