0

I am using URL in d3.json function, the data is showing undefined.

    My code here
    ==============
    var jsonURL = "http://192.168.7.102:8080/restful/traffic/json/get";

    d3.json(jsonURL, function(error, data){

    alert(data);
 });

When it is executed data is showing undefined. I created restful web service application to get the data using above URL it returns JSONArray.

Application Code

@GET
    @Path("/json/get")
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray startReading() throws JSONException {

        String json = null;
        String newJson = null;

        try {

            URL url = new URL("http://192.168.7.102:3000/data/traffic");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            while ((json = br.readLine()) != null) {
                newJson = json;

            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        JSONArray array = new JSONArray(newJson);

        return array;

    }

When I access the URL in the browser data is displaying. But in d3.json function it is showing undefined instead of data. Due to that, I am not able to display the graph in the browser. Please guide me where I was doing wrong.

naresh nandu
  • 137
  • 2
  • 10

1 Answers1

0
var data;

d3.json("http://192.168.7.102:8080/restful/traffic/json/get", function(error, json) {
  if (error) return console.warn(error);
  data = json;
  visualizeit();
});

Have a look at - https://github.com/mbostock/d3/wiki/Requests

Malith
  • 970
  • 7
  • 17