I was hoping to make this example on jquery autocoplete work using a simple custom service I made up, but it's not working at all.
snippet:
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://localhost:8085/TestJsonArrayAJAX/MyServlet",
dataType: "jsonp",
data: {
q: request.term
},
success: function( data ) {
alert("ciao");
response( data );
}
});
},
minLength: 3,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
I noticed that the output of the original service is something like that:
jQuery1113013915240531787276_1443349161768(["Milaca, MN, United States","Milam, TX, United States");
meanwhile my services output is something like:
["Roma","Milano","Napoli","Palermo","Torino","Firenze","Pisa"]
because I though that a json array was enough and apparently is not.
Why is it not working, why a simple json array is not enough ?
Can you please help ?
EDIT:
implementing my service like this:
JSONArray jarr = new JSONArray();
jarr.put("Roma");
jarr.put("Milano");
jarr.put("Napoli");
jarr.put("Palermo");
jarr.put("Torino");
jarr.put("Firenze");
jarr.put("Pisa");
String cb = request.getParameter("callback");
response.getOutputStream().print(cb+"("+jarr.toString()+");");
it works, but I still don't get why ??
First in your code you dont need to call log function as you have not defined in current snipet
second change dataType: "jsonp",
to dataType: "json",
you are providing input as json source not jsonp there is difference in them
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments