How to apply a url using Blade Laravel in javascript

0

I am using typehead mixed with prefecth for the auto completion of a field called rut, at the moment of requesting information from a JSON file if I use the route calling it in the following way:

prefetch: '/ providers / json'

It does not throw me any inconvenience, whereas if I try to respect the blade laravel syntax by calling the url in the following way

prefetch: '{{url (' / providers / json ')}}'

It does not interact with the route, which is defined within my web.php file

This is the code of the JavaScript part

    <script>
    $(document).ready(function(){

        var providers = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,

            prefetch: '{{url('/providers/json')}}'
        });

        $('#rut').typeahead({
            hint: true,
            highlight: true,
            minLength: 3
        },
        {
            name: 'providers',
            source: providers
        })
    });
</script>

Note: I use the version of laravel 5.6

    
asked by Juan Mellado J. 09.04.2018 в 22:47
source

1 answer

0

Try the following in the head:

<meta name="csrf-token" content="{{ csrf_token() }}">

in $(document).ready

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

Alternatively you can exclude certain routes from the middleware that checks the csrf ( VerifyCsrfToken )

link

    
answered by 10.04.2018 в 04:20