I am having trouble on my autocomplete as it is returning only one record whereas the remote webservice is returning 10 items.
Was wondering if you could have a look at my code and see if I’m doing something wrong?
Received data:
{“d”:”[\”02102008633\”,\”02102008794\”,\”02102008980\”,\”02102015321\”,\”02102018743\”,\”02102024602\”,\”02102037454\”,\”02102038366\”,\”02102040774\”,\”02102056369\”]”}
jQuery(txtDestination).autocomplete({
minLength: 2,
source: function (request, response) {
jQuery.ajax({
url: "/SearchService.asmx/GetDestinationAutocompleteValue?" + "accountCode=" + accountCode.toString() + "&criteria=" + jQuery(txtDestination).val().toString(),
data: "{}",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data != null && data.hasOwnProperty('d') && eval(data.d) != null) {
var result = new Array(eval(data.d));
response(jQuery.map(result, function (item, ctr) {
return { label: item[ctr], id: item[ctr], value: item[ctr] }
}));
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus + " " + errorThrown); }
});
}
});
Thanks a million!
Cheers,
Ann
I think I found it… Take a look at this line:
Now, since
eval(data.d)evaluates to an array already, you’re essentially calling something like this:Which will actually create an array of length one – the first element of which is an array of length three. This blindsided me, too, until I thought to check it in a JS console (and don’t get me started on the printout not including square brackets…):
But the good news is that there’s an easy fix:
Hope this helps!