When displaying data from server side why are my accents and 'ñ' showing: �?
When displaying data from server side why are my accents and 'ñ' showing: �?
Hi! I got a table that has data with accents and 'ñ' So my json that is sent from the server shows the accents and 'ñ', but the json that I get in the response shows �. For example in my server side inside the json I have the word MONTAÑA and in the cliente side it shows MONTA�A.
Here is my code for the client:
eventosOperativos = $("#eventosOperativos").dataTable({
"language": { url: '<%=request.getContextPath()%>/js/dataTables/espanol.json'},
"dom": '"top"i',
"scrollY": "310px",
"bServerSide": true,
"sAjaxSource": "<%=request.getContextPath()%>/calendarioAction!getListCalendarioOperativoDia.action",
"bProcessing": true,
"aoColumns": [
{"mDataProp": "division"},
{"mDataProp": "distrito"},
{"mDataProp": "zona"},
{"mDataProp": "anFisCamp"},
{
"mDataProp": "fechaIniciaSCal",
"mRender": function (data, type, full) {
return "
<input style='max-width: 88px; float:left;' type='text' class='fechaInicioUpd editable' placeholder='yyyy-MM-dd' value='" + data + "'/>";
},"bSortable": false
},
{
"mDataProp": "fechaTerminaSCal",
"mRender": function (data, type, full) {
return "
<input style='max-width: 88px; ; float:left;' class='fechaTerminaUpd editable' placeholder='yyyy-MM-dd' value='" + data + "'/>";
},"bSortable": false
},
{
"mRender": function (data, type, full) {
return "
";
},"bSortable": false
}
],"columnDefs": [
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"},
{ "width": "10%"}
],
"tableTools": {
"sSwfPath": "<%=request.getContextPath()%>/js/dataTables/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
"aButtons": [{"sExtends": "copy","sButtonText": "Copiar a portapapeles"},
{"sExtends": "csv","sButtonText": "Guardar todo Excel"},
{"sExtends": "xls","sButtonText": "Guardar selección Excel","oSelectorOpts": {page: 'current'}},
{"sExtends": "print","sButtonText": "Vista de impresión"}],
"sRowSelect": "single"
});
and in my server side I have:
public String getListCalendarioOperativoDia() {
JQueryDataTableParamModel param = DataTablesParamUtility.getParam(super.getServletRequest());
try {
json = new StringBufferInputStream(eventosService.getListCalendarioOperativoDia(param));
} catch (JsonIOException e) {
logger.error(Mensajes.ERROR_OBTENIENDO_EVENTOS2);
json = new StringBufferInputStream("error");
} catch (Exception e) {
logger.error(Mensajes.ERROR_OBTENIENDO_EVENTOS2);
json = new StringBufferInputStream("error");
}
return ActionsConstants.AJAX;
}
Thanks in advance!
Replies
On the server-side, make sure your data is being hit with
utf8_encode()
and that should fix it.I´m forcing my response to be utf-8
Here is my whole controller class:
package com.proximate.www.pushmate.action;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.proximate.www.pushmate.constants.ActionsConstants;
import com.proximate.www.pushmate.constants.Mensajes;
import com.proximate.www.pushmate.dao.ICampaignDAO;
import com.proximate.www.pushmate.dao.IEventosDAO;
import com.proximate.www.pushmate.dao.IZonaDAO;
import com.proximate.www.pushmate.dto.Evento;
import com.proximate.www.pushmate.service.IEventosService;
import com.proximate.www.pushmate.tableutils.DataTablesParamUtility;
import com.proximate.www.pushmate.tableutils.JQueryDataTableParamModel;
import com.proximate.www.pushmate.utils.GenerarJSONSalida;
@SuppressWarnings("deprecation")
@ParentPackage("mi-paquete")
@Action(value="calendarioAction")
@Results({
@Result(name=ActionsConstants.INICIAR, type="tiles", location=ActionsConstants.URL_CALENDARIO_ACCION),
@Result(name=ActionsConstants.ERROR, type="tiles", location=ActionsConstants.URL_ERROR),
@Result(name = ActionsConstants.AJAX, type = "stream", params = {
"contentType",
"application/json;charset=UTF-8",
"inputName",
"json"})
})
public class CalendarioAction extends BaseAction {
}
I'm not sure what the issue would be then if you are sure that is doing it. But if it was being UTF8 encoded, I don't see how you'd be getting the �. I just tried doing it with my server-side code and using the ñ would cause my response to have � in it, and the table would never load. Then I threw a utf8_encode around it and everything worked fine. I would at least try it since it only takes a minute and you can for sure eliminate that as a possibility.
If it still doesn't work then hopefully someone else can help.
how do you use utf8_encode ???