I am calling a servlet to get list of objects from database and the list am returning as json from the servlet. Same json response will be displayed in jsp table as below.
Servlet code:
String json = new Gson().toJson(resultList);
response.setContentType("application/json"); //here i have the data and i came to know by debugging
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
jquery code:
$.ajax({
type: "GET",
url: "./dataFetchController",
success: function(responseJson) {
console.log(responseJson); //it is not printing any result on firebug console
var master = $(this).parents("table.myTable");
$.each(responseJson, function(index, contact) { // Iterate over the JSON array.
// Get a new row based on the prototype row
var prot = master.find(".prototype").clone();
prot.attr("class", "");
prot.find("#myName").attr("value", contact.name);
prot.find("#myLastName").attr("value", contact.lastName);
//master.find("tbody").append(prot);
jQuery('table.myTable tr:last').before(prot);
});
},
error: function(ob,errStr) {
$('#contactForm #formProgress').html('');
$('#contactForm #formProgress').html('<img src="./public/images/error.png" /> <span style="color:red">Save is not successful. Try Again.</span>');
}
});
I am not getting any json result. I tried printing it using console but no result but in servlet it has data returned from database and verified using debug. Am i missing anything here?
Thanks!
I could not see "dataType" in Your Ajax request like:
dataType: 'json'
try adding it.
so, it will become:
$.ajax({
type: "GET",
url: "./dataFetchController",
success: function(responseJson) {
//alert and success handler
},
dataType: 'json',
error : function(){
// error handler
});
dataType is the type of data that you're expecting back from the server
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.