问题
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 & the json I used..
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......
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function doajax(){
$.ajax({
url: "AccountServlet",
type: "post",
dataType: "json",
error:function(){
alert("error occured!!!");
},
success:function(data){
alert(data.fullName + "\n" + data.mobileNo);
}
});
}
</script>
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 ...
IN the accountServlet i am creating the json like this
ArrayList<DistinctSourceCount> countList = new ArrayList<DistinctSourceCount>();
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 < 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();
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
回答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.
}
});
来源:https://stackoverflow.com/questions/19499572/how-to-return-an-json-object-and-catch-in-in-ajax-call-in-html