Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

ajax - jQuery UI Autocomplete JSON gives error: Uncaught TypeError: Cannot use 'in' operator to search for '62' in

I am having a great deal of trouble getting autocomplete to work on my page. When I enter 2 characters ("OW") into my search input, I get the preloader image (see below), but it never goes away and I never get the autocomplete popup. Looking at the console in Chrome shows:

Uncaught TypeError: Cannot use 'in' operator to search for '62' in [{"value":103,"label":"FLOWER"},{"value":105,"label":"YELLOW"}] 

Here is the actual string that is being returned (confirmed by adding an alert(data) in the success block):

[{"kwrdID":103,"kwrdKeyWord":"FLOWER"},{"kwrdID":105,"kwrdKeyWord":"YELLOW"}]

Here is the main code for the autocomplete

$("#searchInput").autocomplete({
source: function (request, response) {
    $.ajax({
        url: '@Url.Action("GetKeywords", "Home")',
        dataType: "json",
        data: {
            SearchTerm: request.term
        },
        success: function (data) {
            response($.map(data.keywords, function (item) {
                return {
                    label: item.kwrdKeyWord,
                    value: item.kwrdID
                }
            }));
        }
    });
},
    minLength: 2
});

And finally, here is the preloader (just in case it's related).

$(document).ajaxStart(function () {
    var position = $('#divParent').position();
    position.left += (($('#divParent').width() / 2) - ($('#preloader').width() / 2));
    position.top += (($('#divParent').height() / 2) - ($('#preloader').height() / 2));
    $('#preloader').css(position).show();
    $('#preloader').show();
}).ajaxStop(function () {
    $('#preloader').hide();
});

Can anyone help to explain what's going on here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It was a long road, but after many hours of experimenting I came up with this code:

$("#searchInput").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '@Url.Action("GetKeywords", "Home")',
            dataType: "json",
            data: {
                SearchTerm: request.term
            },
            success: function (data) {
                var parsed = JSON.parse(data);
                var newArray = new Array(parsed.length);
                var i = 0;

                parsed.forEach(function (entry) {
                    var newObject = {
                        label: entry.kwrdKeyWord
                    };
                    newArray[i] = newObject;
                    i++;
                });

                response(newArray);
            },
            error: function (message) {
                response([]);
            }
        });
    },
    minLength: 2
});

This appears to work fine. The truth is my keywords are unique, so I can live without the ID anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...