0
votes

my controller:

jjgp_bisa_bis_crea_mensaje: pattern: /creamensaje defaults: { _controller: JJGPBisaBisBundle:Default:creamensaje }

my Action function

public function creamensajeAction() {

    $request = Request::createFromGlobals();

    $usermail = $request->request->get('usermail');
    $mensaje = $request->request->get('mensaje');
    $leido = $request->request->get('leido');
    $fecha = date("d-m-Y H:i:s");

    $em = $this->getDoctrine()->getManager();
    $mensajes = $em->getRepository('JJGPBisaBisBundle:Mensajes')->CrearMensajes($usermail, $mensaje, $fecha, $leido);
    $em = $this->getDoctrine()->getManager();
    $em->persist($mensajes);
    $em->flush();

    // response test

    $return['usermail'] = 'a';
    $return['mensaje'] = "b";
    $return['leido'] = "c";

    return json_encode($return);
}

my jquery:

$.ajax({
    type: 'POST',
    url: '{{ path('jjgp_bisa_bis_crea_mensaje') }}',
    dataType: 'json',
    data: {    // test vars only
        usermail: "email",
        mensaje: $txt_enviar ,
        leido: "no"
    },
    success: function(data) {
        alert(data.msg);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert("error"); // always jump here!! that's the problem!
    }
});

All of this seems work ok, I can see how the database is growing up and works fine, but jquery always alerts me error function, and I don't know why!

I think it can be the controller path but I am not sure

1

1 Answers

0
votes

Your action has to return some sort of Response object. In your case, you can use a JsonResponse. include this statement:

use Symfony\Component\HttpFoundation\JsonResponse;

And the you can replace this line

return json_encode($return);

with this:

return new JsonResponse($return);

You may also want to look at what error is thrown. Without the error message, it is kinda hard to see exactly what the problem is.