Problem with the use of symfony path in jquery ajax call

0

I am developing a web system with Symfony 3.4. I have a function that must execute an ajax call to a controller and use the jquery function for that purpose. I'm having problems using the {{path ('')}} in the url The ajax call is the following in my file turnos_filtrar.twig.js :

        $.ajax
    ({
        async:true,
        method: 'GET',
        url: "{{ path('turnos_listar') }}",
       //url: '/turnos/listar',
        data: datos,
        dataType: 'json',
        beforeSend:iniciaEnvioTurnos,
        success: cargarFilasTurnos,
        timeout:11500,
        error : function() 
        {
            //desbloqueo la pagina
            $.unblockUI();

            //accedo al alert
            //var error = document.getElementById('error-turno');
            //seteo el msj
            //error.innerHTML = '<p>Error de conexión, por favor intente registrarse nuevamente más tarde</p>';
            //muestro
            //$('#error-turno').show();
            alert('ERROR DE CONEXIÓN, INTENTE NUEVAMENTE MAS TARDE');
        }
    });

The route you are consulting is / shifts / listing and debugging: routing in console I get

 turnos_listar              GET      ANY      ANY    /turnos/listar

The call works if I use the line that is commented:

 //url: '/turnos/listar',

But it does not work when I use the {{path ('')}}, I am interested in using it for changes in the routing, it is not necessary to manually change the js files. Thank you! Your help is welcome! Cristian

    
asked by Cristian Budzicz 11.10.2018 в 18:30
source

2 answers

0

Hi, I was able to solve it in the following way: In the template twig I included the following code:

{% block javascripts %}
    <script>
        {% include 'scripts/panel turnos/turnos_filtrar.twig.js' %}
    </script>
{% endblock %}

The project structure was modified, adding a folder named scripts where all the necessary ajax calls will be located. In this directory, the file shifts_filter.twig.js is added. I copy an image of how the dir is structured

To call the file where I have the ajax call. Modify the url of the ajax function to the following form:

 $.ajax
    ({
        async:true,
        method: 'GET',
        url: "{{ path('turnos_listar') }}",

As can be seen, I can now use the {{path ('')}} of twig. In addition, the netbean recognizes the syntax of both javascript, jquery and twig, which is very good. I hope you find it useful.

    
answered by 12.10.2018 / 22:11
source
0

I think the problem lies in the extension of the file. The file should end with the .twig extension like this:

turnos_filtrar.js.twig

I have always worked in this way and have never had any problems.

Greetings.

    
answered by 11.10.2018 в 20:41