Autocomplete with typeahead and JSON

Asked

Viewed 134 times

1

I have an ASP.NET MVC project and in it a script for autocomplete using typeahead with Prefetch:

$(document).ready(function () {
            // Sonstructs the suggestion engine
            var ativos = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.whitespace,
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                // The url points to a json file that contains an array of country names
                prefetch: 'ativos.json'
            });

            // Initializing the typeahead with remote dataset
            $('.typeahead').typeahead(null, {
                name: 'ativos',
                source: ativos,
                limit: 7 /* Specify maximum number of suggestions to be displayed */
            });
        });

And the JSON file in the same folder, in the form: ["petr4","usim5","itub4"]the error I’m getting on the console :

Failed to load Resource: the server responded with a status of 404 (Not Found)

I don’t understand why the script can’t get the JSON file. I think I must make some call via AJAX to return this JSON but I do not know how it would look. I thank you from now on.

1 answer

0


Put the URL starting from root (webroot address) of your server. Use this syntax also inside the object you pass to the Bloodhound constructor:

prefetch: {url : 'path/to/ativos.json'}
  • AEEE CARA, it worked -, but I had to put the JSON file on the project root!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.