jQuery UI的自动完成JSON给出错误:未捕获的类型错误:无法使用“的”运营商在寻找'62' [英] jQuery UI Autocomplete JSON gives error: Uncaught TypeError: Cannot use 'in' operator to search for '62' in

查看:393
本文介绍了jQuery UI的自动完成JSON给出错误:未捕获的类型错误:无法使用“的”运营商在寻找'62'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的麻烦自动完成的工作,在我的网页。当我输入两个字符(OW)为我的搜索输入,我得到了preloader图像(见下文),但它永远不会消失,我从来没有得到自动完成弹出。看着在Chrome控制台显示:

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"}]

下面是主要的$ C $下自动完成

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
});

最后,这里是preloader(以防万一它相关的)。

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?

推荐答案

这是一条漫长的道路,但几个小时的实验后,我想出了这个code:

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.

这篇关于jQuery UI的自动完成JSON给出错误:未捕获的类型错误:无法使用“的”运营商在寻找'62'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆