0

I try to use JQuery autocomplete and I get this error:

Uncaught SyntaxError: Unexpected token < in JSON at position 2

My code is like this, the 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>

The javascript:

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

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

                    }});

I have no idea why but changing sourceto lookup made it works

3
  • this issue is familiar to me, are you sure you're not using some php within your code somewhere? Commented Sep 23, 2016 at 21:22
  • Yes, I think maybe the source is not good Commented Sep 23, 2016 at 21:32
  • Change to "lookup" and in your json check that their is no empty values Commented Sep 23, 2016 at 21:38

1 Answer 1

1

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

The autocomplete expects an array in the above format. Change your array to the following:

<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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Here is a jsfiddle link with working code. jsfiddle.net/9roepp5z/1 You can use the structure you had, but the drop down uses your data column for the display and the value column for what it places into the textbox upon selection. You did have an unnecessary closing INPUT tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.