for several days I have the problem, that when I call the jquery autocomplete function it does not show me anything, and I have already reviewed the questions that others have already formulated, but I still do not realize what the problem is, I suppose that because I am not very clear JavaScript. Thanks in advance to anyone who can help me out!
The function is working, in fact I have tried to follow it with the debugger and it seems to work. In my view, a JSP page, I have this code:
<script language="javascript" type="text/javascript">
$(function () {
$("#cuenta").autocomplete({
source: function (request, response) {
alert("pepe");
$.ajax({
type: "POST",
url: "CuentaBusca",
contentType: "application/json; charset=utf-8",
dataType: "json",
minLength: 2,
delay: 100,
success: function (data) {
console.log( data);
response(data);
},
select: function (event, ui) {
return false;
}
});
}
});
});
</script>
The console.log shows me the following output:
And in the servlet I have the following code, which also works:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
DBConexion conex = null;
try {
conex = new DBConexion();
} catch (SQLException ex) {
throw new ServletException("Sucedio un Error al Realizar la Conexion a la Base de Datos !", ex);
}
Connection conn = conex.getConexion();
try {
PreparedStatement ps = conn.prepareStatement("select id,nombre from cuentas order by nombre");
ResultSet rs = ps.executeQuery();
JsonObject json_response = new JsonObject();
JsonArray data_json = new JsonArray();
while (rs.next()) {
JsonObject json = new JsonObject();
json.addProperty("id", rs.getInt("id"));
json.addProperty("nombre", rs.getString("nombre"));
data_json.add(json);
}
json_response.add("aaData", data_json);
response.setContentType("application/Json");
response.getWriter().write(json_response.toString());
} catch (SQLException ex) {
throw new ServletException("Sucedio un Error al Realizar la Conexion a la Base de Datos !", ex);
}
// Cierra la Conexion
try {
conex.closeConexion();
} catch (SQLException ex) {
throw new ServletException("Sucedio un Error al Cerrar la Conexion en la Base de Datos !", ex);
}
}
I suspect that I am returning in the wrong format from the servlet, but I couldn't find a clearer explanation. Any ideas?
Regards, Ferdinand
Finally I was able to find the solution, I did not understand how the format should be in the "success" section: