/ / Autocomplete.js Sugestie? - javascript, jquery, ajax, autouzupełnianie

Autocomplete.js Sugestie? - javascript, jquery, ajax, autouzupełnianie

Mam wtyczkę autouzupełniania (Autouzupełnianie DevBridge) na moim polu tekstowym.

$("#myTextBox").autocomplete({
serviceUrl: "/Handler/Autocomplete.ashx?"
});

Wykonuje wywołanie Ajax (widzę zwrot JSON w skrzypku) i otrzymuję zwrot w następujący sposób:

[{"Key":39,"Value":"118"},{"Key":40,"Value":"155"},{"Key":2,"Value":"16"}]

ale ciągle pojawia się błąd:

Unable to get property "length" of undefined or null reference

W tej części kodu:

verifySuggestionsFormat: function (suggestions) {
// If suggestions is string array, convert them to supported format:
if (suggestions.length && typeof suggestions[0] === "string") {
return $.map(suggestions, function (value) {
return { value: value, data: null };
});
}

return suggestions;
}

Nie jestem pewien, co to oznacza.Czy ktoś mi powiedzieć, jak to naprawić? Czy to po prostu składnia? Nie jestem pewien gdzie / jak dodać te sugestie ...

Odpowiedzi:

0 dla odpowiedzi № 1

suggestions ma wartość null lub undefined.

Przed sprawdzeniem zmiennej sprawdź, czy istnieje:

if (!suggestions) return;

Na przykład:

verifySuggestionsFormat: function (suggestions) {

// Fail fast if suggestions is not valid
if (!suggestions) return;

// If suggestions is string array, convert them to supported format:
if (suggestions.length && typeof suggestions[0] === "string") {
return $.map(suggestions, function (value) {
return { value: value, data: null };
});
}

return suggestions;
}