Tuesday, 13 August 2013

Using jquery ui autocomplete + ajax json data

Using jquery ui autocomplete + ajax json data

I'm trying to use http://jqueryui.com/autocomplete/#remote-jsonp
I've taken the code and tried to adapt it. With the orginal code it works
(I can get the towns), and when I adapt to my data, it doesn't. I get json
data from a php file. Here is my code:
jquery:
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
console.log(request.term);
$.ajax({
type: "POST",
url: "/chercheabo",
dataType: "jsonp",
data: {
achercher: request.term
},
success: function( data ) {
response( $.map( data.myData, function( item ) {
return {
label: item.pseuDO + (item.pseuDO ? ", " + item.userID :
"") + ", " + item.pseuDO,
value: item.pseuDO
}
}));
}
});
},
minLength: 2,
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" );
}
});
My html:
<div class="ui-widget">
<label for="city">Your city: </label>
<input id="city" />
Powered by <a href="http://geonames.org">geonames.org</a>
</div>
<div class="ui-widget" style="margin-top: 2em; font-family: Arial;">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;"
class="ui-widget-content"></div>
</div>
My json code return:
{"myData":[{"userID":"11","pseuDO":"toto"},{"userID":"20","pseuDO":"blogo"}]}
Can you see anything wrong in my code?... Thanks

No comments:

Post a Comment