title: How to return an json Object and catch in in Ajax call in HTML
body: <p>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 data.Now I have a couple of information that will come from database and hit the Servlet. I have to make the json dynamically in the Servlet so that it can change upon user response.Here is the  script &amp; the json I used..</p>
  
  <p>This is my HTML file from where I made an Ajax call to the Servlet. The Ajax is calling the Servlet 
  ,but there is an error in getting back the response. I am getting an "error occurred" message.When I am running the servlet......  </p>
  
  <pre><code> &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
      &lt;script&gt;            
          function doajax(){
              $.ajax({
                  url: "AccountServlet",
                  type: "post",
                  dataType: "json",
  
                  error:function(){
                      alert("error occured!!!");
                  },
                  success:function(data){
                      alert(data.fullName + "\n" + data.mobileNo);
                  }
              });
          }
      &lt;/script&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is the Html from where i am tring to get the response back
  this file just calls a Servlet.But in the response i am getting a error occuree message ...</p>
  
  <p>IN the accountServlet i am creating the json like this</p>
  
  <pre><code>     ArrayList&lt;DistinctSourceCount&gt; countList = new ArrayList&lt;DistinctSourceCount&gt;();
          countList.add(new DistinctSourceCount("Jan", 1800));
          countList.add(new DistinctSourceCount("Feb", 1500));
          countList.add(new DistinctSourceCount("March", 2000));
          countList.add(new DistinctSourceCount("April", 1550));
          countList.add(new DistinctSourceCount("May", 1000));
          countList.add(new DistinctSourceCount("June", 1700));
          countList.add(new DistinctSourceCount("July", 1400));
          countList.add(new DistinctSourceCount("Aug", 1900));
          countList.add(new DistinctSourceCount("Sept", 1000));
          countList.add(new DistinctSourceCount("Oct", 1500));
          countList.add(new DistinctSourceCount("Nov", 1100));
          countList.add(new DistinctSourceCount("Dec", 2000));
  
          Gson gson = new Gson();
          JsonArray arrayObj = new JsonArray();
          for (int i = 0; i &lt; countList.size(); i++) {
              DistinctSourceCount count = countList.get(i);
              JsonElement linObj = gson.toJsonTree(count);
              arrayObj.add(linObj);
          }
  
          JsonObject myObj = new JsonObject();
          myObj.addProperty("success", true);
          myObj.add("topList", arrayObj);
          myObj.addProperty("totalCount", countList.size());
  
          System.out.println(myObj.toString());
          System.out.close();
  </code></pre>
  
  <p>This is my code I have done so far.Can anyone help me about how to create the json object in Servlet and getting the  response back the script??please anyone help</p>