JQuery 自动完成 json 解析错误

JQuery autocomplete json parse error

我尝试使用 JQuery 自动完成,但出现此错误:

Uncaught SyntaxError: Unexpected token < in JSON at position 2

我的代码是这样的,HTML:

<div id="fastSearchBox" class="fastSearchBox">
            <span style="direction: rtl; float: right;">search</span>
            <input id="fastSearchInput" type="text" style="width: 150px; margin-right: 10px;"></input>
        </div>

javascript:

users = [{"data":1,"value":"foo"}];

                    $("#fastSearchInput").autocomplete({   
                    source: users,
                    select: function (event, ui)
                    {

                    }});

我不知道为什么,但将 source 更改为 lookup 使其有效

An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

自动完成需要上述格式的数组。将您的数组更改为以下内容:

<body>
    <div id="fastSearchBox" class="fastSearchBox">
    <span style="direction: rtl; float: right;">search</span>
    <input id="fastSearchInput" type="text" style="width: 150px; margin-right: 10px;">
    </div>
</body>

var users = [ 
    {data: 1, value: "foo" },
    {data: 2, value: "foo2" },
    {data: 3, value: "foo3" }
];

$( "#fastSearchInput").autocomplete({
    source: users
});

$( "#fastSearchInput").on( "autocompleteselect", function( event, ui ) {
    alert(ui.item.data);
});