Skip to content Skip to sidebar Skip to footer

How To Return An Json Object And Catch In In Ajax Call In HTML

I have a simple tree structure of information in my D3 code. The D3 script is calling json from the script d3.json() function and the resulting json is giving the tree structure d

Solution 1:

You have to create json Object in the servlet. And then retrive that json using ajax call in your script. For creating json in servlet you can use any library like Gson or java-json.

For example:

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","flare");
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject_child = new JSONObject();
        jsonObject_child.put("name", "analytics");
        jsonArray.put(jsonObject_child);
        jsonObject.put("children",jsonArray);
        System.out.println(jsonObject);

And the ajax code as below:

        $.ajax({
        type: "POST",
        url: "PATH_OF_SERVLET",
        dataType: 'json',
        success: function(response) {
        // Parse your response from Servlet here.
        }

    });

Post a Comment for "How To Return An Json Object And Catch In In Ajax Call In HTML"