json problmen ajax with servlet
json problmen ajax with servlet
I have a problem, I explain:
1. I try connect datatable with json and ajax, this is function ok, the code connect with servlet, code below:
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aaSorting": [[ 2, "desc" ]],
"aoColumns": [
{"sClass": "center", "bSortable": false},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": false }],
"sScrollX": "100%",
"sScrollXInner": "140%",
"bScrollCollapse": true,
"bJQueryUI": true,
"bServerSide": true,
"sAjaxSource":"http://xx.xx.xx.xx:yyyy/myservlet/getServletjson",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( {"name": "TCI", "value": "" } ); // <-- this
aoData.push( {"name": "TCF", "value": "" } ); // <-- this
aoData.push( {"name": "Status", "value": "" } ); // <-- this
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": function(json){
var json = json = eval('('+ json.result.model + ')');
alert("json: "+ json);
//var test = json;
//alert("test: "+ test);
fnCallback(json);
}
"error": function (xhr, error, thrown) {
if (error == "parsererror") {
alert("DataTables warning: JSON data from server could not be parsed. " +
"This is caused by a JSON formatting error.");
}
alert(error);}
});},
"bProcessing": true,
"sPaginationType": "full_numbers" } );
2.- But when servlet return json ( this example aaData is correctly ) not function
try {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("sEcho", sEcho);
jsonResponse.put("iTotalRecords", iTotalRecords);
jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
JSONArray row = new JSONArray();
row.put("checkbox").put("20011-1").put("Internet Explorer 4.0").put("Win 95+").put("4").put("XS");
data.put(row);
jsonResponse.put("aaData", data);
PrintWriter out = resp.getWriter();
resp.setContentType("application/json");
out.print(jsonResponse.toString());
out.close();
} catch (Exception e){
e.printStackTrace();
resp.setContentType("text/html");
resp.getWriter().print(e.getMessage());
}
return;
My problem is that when servlet response this string, my datatable not show the response and I don't know that occurr.
I need this resolve the problem
Tranks for all
1. I try connect datatable with json and ajax, this is function ok, the code connect with servlet, code below:
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aaSorting": [[ 2, "desc" ]],
"aoColumns": [
{"sClass": "center", "bSortable": false},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": true},
{"sClass": "center", "bSortable": false }],
"sScrollX": "100%",
"sScrollXInner": "140%",
"bScrollCollapse": true,
"bJQueryUI": true,
"bServerSide": true,
"sAjaxSource":"http://xx.xx.xx.xx:yyyy/myservlet/getServletjson",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( {"name": "TCI", "value": "" } ); // <-- this
aoData.push( {"name": "TCF", "value": "" } ); // <-- this
aoData.push( {"name": "Status", "value": "" } ); // <-- this
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": function(json){
var json = json = eval('('+ json.result.model + ')');
alert("json: "+ json);
//var test = json;
//alert("test: "+ test);
fnCallback(json);
}
"error": function (xhr, error, thrown) {
if (error == "parsererror") {
alert("DataTables warning: JSON data from server could not be parsed. " +
"This is caused by a JSON formatting error.");
}
alert(error);}
});},
"bProcessing": true,
"sPaginationType": "full_numbers" } );
2.- But when servlet return json ( this example aaData is correctly ) not function
try {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("sEcho", sEcho);
jsonResponse.put("iTotalRecords", iTotalRecords);
jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
JSONArray row = new JSONArray();
row.put("checkbox").put("20011-1").put("Internet Explorer 4.0").put("Win 95+").put("4").put("XS");
data.put(row);
jsonResponse.put("aaData", data);
PrintWriter out = resp.getWriter();
resp.setContentType("application/json");
out.print(jsonResponse.toString());
out.close();
} catch (Exception e){
e.printStackTrace();
resp.setContentType("text/html");
resp.getWriter().print(e.getMessage());
}
return;
My problem is that when servlet response this string, my datatable not show the response and I don't know that occurr.
I need this resolve the problem
Tranks for all
This discussion has been closed.