I’m trying to read in JSON objects over a websocket for visualisation by the D3 library. I’m just checking if the browser receives the JSON correctly. This is the code:
ws = new WebSocket("ws://localhost:8888/dh");
ws.onmessage = function(evt)
{
d3.json(
JSON.parse(evt.data),
function (jsondata) {
console.log(jsondata);
var data = jsondata.map(function(d) { return d.Value; });
console.log(data);
});
}
But when I check the console log it gives me these errors:
[19:15:36.434] TypeError: jsondata is null @ http://localhost:8888/static/anEx.js:21
and console.log(data) is null.
What is incorrect here?
Thanks
d3.jsonis used to make HTTP requests that return JSON responses.You already have the data in your WS message handler, there’s no need to make another request. Just do: