AJAX response problem with Symfony2 (PHP)

1

I have a problem with the code:

$cadenaScroll_respuesta.="<li> <div id='usuario-respond'><div id='imagen-usuario-respond' ><ul> <li id='nombreUsuario'> <b>".$respuesta->getUsuario()." </b></li><li id='avatar'> <img src = {{ asset('bundles/UsuarioBundle/Uploads/' ~ ".$respuesta->getFoto()." ~' ) }}'  width='100%'  height='100%'/> </li> <li id='opciones'><b>  opciones </b> </li> </ul></div>".$respuesta->getTexto()."</div></li>";
    }

The previous string is returned with a response from the controller, and basically I need it to make the append of the li to an ul (they are forum topics that are updated by scrolling on Ajax request).

in my jquery success function

success: function(data) {

                    alert(data);
                    $('#loading').hide();
                    //window.scrollTo(50, 0);
                    $('#detalle ul').append(data);
                },

in the browser does not recognize the dir of Asset (when I give inspect element appears to me)

<img src="{{" asset('bundles="" usuariobundle="" uploads="" '="" ~="" 1.jpg="" ~'="" )="" }}'="" width="100%" height="100%">

with which I sense that you do not recognize the symbols / and puts me quotes again

    
asked by Ali Mendoza 23.05.2016 в 20:48
source

2 answers

1

It is as you explained above, in your controller you should have the answer, say:

return $this->render('UsuarioBundle:Usuario:respuesta.html.twig', array('respuesta'=>$respuesta));

and the respuesta.html.twig fragment will contain your code replacing the php $respuesta variable with the corresponding twig variable.

    
answered by 10.04.2017 в 04:15
0

You are using twig code inside a string in php but for the twig interpreter to run you must use the "render" method of the controller and have the code twig in a view.

It is not Jquery's task to translate these blocks and functions. You should get exactly the html you want to add to the page.

    
answered by 19.07.2016 в 16:02