How to handle json returned to autocomplete from servlet
I'm trying to use the autocomplete widget to and to populate it I am using a servlet which is returning a JSON array. Bear with me, I'm not sure how much you need to see so I'm going to include a lot.
Here is how I am constructing the autocomplete right now. It's not complete yet because I don't really know how to finish:
$('#usernamesearch').autocomplete({
minLength: 2,
source: function(request, response) {
$.ajax({
url: "./UserServlet?action=search",
dataType: "json",
data: request,
success: ""
});
}
});
Clearly I need to work on success, that's where my hold up is right now. Here is the servlet code that builds the JSON array. The data is coming out of a database using jdbc:
ResultSet result = stmt.executeQuery();
JSONArray array = new JSONArray();
while (result.next()) {
JSONObject obj = new JSONObject();
obj.put("fullname", result.getString("fullname"));
array.add(obj);
}
result.close();
stmt.close();
log.info(array.toJSONString());
out.print(array.toJSONString());
out.flush();
My output from this block of code shows this:
[{"fullname":"andrea danziger"},{"fullname":"Stacey Moda"},{"fullname":"Debbie Jordan"},{"fullname":"Greg Jordan"},{"fullname":"Janet Randall"},{"fullname":"Arjun Ghosh-Dastidar"},{"fullname":"Brian Adamson"}]
This looks like a good JSON array to me, although I'll be the first to admit that I'm not an expert.
So, what's the next step? The data is returning, I can see that clearly enough, but how do I parse it and return it to the autocomplete?
I am happy to return the JSON array however it needs to be constructed. I can also modify the options for the autocomplete.
Thanks in advance.