I'm using the jQuery UI autocomplete to show suggestions for a text input field. The suggestions are stored in a Javascript array called suggestions.
I'm trying to fetch the string values for the suggestions array from a database, but I can't convert the List object to a Javascript array.
The Javascript:
var suggestions = [];
$.get('/mywebapp/autocompleteplayer.html', function(data){
parsed = JSON.parse(data);
suggestions = parsed.split(",");
}, "json");
$('#autocompleted').autocomplete({
data: suggestions,
minLength: 3
});
The Spring MVC controller:
@Controller
public class AutocompletePlayerController {
@RequestMapping(value = "/autocompleteplayer")
public List<String> getPlayerSuggestions(){
List<String> myList;
//code that fills myList with all of the players' full names from the database, omitted for brevity
return myList;
}
}
I know I'm not parsing the AJAX response properly, since I've checked from the browser console that the suggestions array has 0 elements. Can anyone help me? What am I doing wrong here?
datain the browser console? If you give us the value of that, it will make it easier for us to help.