Autocomplete with typeahead and JSON

1

I have a project in ASP.NET MVC 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, of the form: ["petr4","usim5","itub4"] the error that I am receiving in the console:

  

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

I'm not understanding why the script can not get the JSON file. I think I should have to make some call via AJAX to get this JSON back but I do not know what it would look like. Thank you in advance.

    
asked by anonymous 19.12.2016 / 13:30

1 answer

0

Place the root URL (webroot address) of your server. Use this syntax too within the object you pass to the Bloodhound builder:

prefetch: {url : 'path/to/ativos.json'}
    
19.12.2016 / 14:49