javascript - poltergeist: jquery ajax does not accept one of two equals responses

950

working with symfony 2.8 I'm trying to make an AJAX call to a controller that returns a JSON that contains one array.

CONTROLER:

public function seguimientoAction(Request $request){
    $idUnico = $request->query->get('plan-id'); 
    $em = $this->getDoctrine()->getManager();
    $idPlan =  $em->getRepository('UsuariosBundle:Plan')->findOneByIdUnico($idUnico);
    $seguimientos = $em->getRepository('UsuariosBundle:Seguimiento')->findByPlan($idPlan);


    $contenedor = array();
    foreach ($seguimientos as $seguimiento){
        $fecha = $seguimiento->getFecha();
        $fecha = $fecha->format('d-m-y');

        $peso = $seguimiento->getPeso();

        $contenedor[] = ['fecha' => $fecha, 'peso' => $peso];
    }

    return new JsonResponse($contenedor);
 }

JAVASCRIPT:

var selectorSeguimiento = $(".plan-seguimiento"); 

    selectorSeguimiento.click(function(event){
        event.preventDefault();

        var planid = $(this).attr("data-id");

            $.ajax({
                type: "POST",
                dataType: 'json',
                ContentType: 'application/json',
                url: Routing.generate('usuarios_dashboard_planes_seguimiento', {planid}),
                success: function(data){
                    console.log(data);

If I write the url that call the controller in the browser I can see this:

[{"fecha":"26-02-18","peso":"67.0"},{"fecha":"28-02-18","peso":"66.0"},{"fecha":"03-03-18","peso":"64.0"}]

So, I understand that the problem is that javascript doesn't take the data..., but... ...If I change the array in controller and I make an identical array "artesanal way"...

CONTROLER(test):

{-code-3}   

...I receive this:

Array(3) [ {…}, {…}, {…} ] localhost:90:25

Array(3) [ "67.0", "66.0", "64.0" ] localhost:99:25

Array(3) [ "26-02-18", "28-02-18", "03-03-18" ] localhost:100:25

And for additional check, I turn back to call with the browser and I obtain this:

[{"fecha":"26-02-18","peso":"67.0"},{"fecha":"28-02-18","peso":"66.0"},{"fecha":"03-03-18","peso":"64.0"}]

Exactly the same that the other case

So, the question is, Why does jquery ajax method "likes" the response with the second array and it "dislikes" the first one (being the same array)?

EDIT: Here you can see a var_dump() of the arrays before the json_encode():

Array $contenedor

{-code-4}

Array $miarray

{-code-4}
108

Answer

-->LOG 1 var arrayPeso = []; var arrayFecha = []; for(var i in data){ arrayPeso.push(data[i].peso); arrayFecha.push(data[i].fecha); } console.log(arrayPeso);
438

Answer

---->LOG 2 console.log(arrayFecha);
911

Answer

--------->LOG 3 } }); });|||public function seguimientoAction(Request $request){ $idUnico = $request->query->get('plan-id'); $em = $this->getDoctrine()->getManager(); $idPlan = $em->getRepository('UsuariosBundle:Plan')->findOneByIdUnico($idUnico); $seguimientos = $em->getRepository('UsuariosBundle:Seguimiento')->findByPlan($idPlan); $miarray = array( array('fecha'=>'26-02-18', 'peso'=>'67.0'), array('fecha'=>'28-02-18', 'peso'=>'66.0'), array('fecha'=>'03-03-18', 'peso'=>'64.0'), ); return new JsonResponse($miarray); }|||array(3) { [0]=> array(2) { ["fecha"]=> string(8) "26-02-18" ["peso"]=> string(4) "67.0" } [1]=> array(2) { ["fecha"]=> string(8) "28-02-18" ["peso"]=> string(4) "66.0" } [2]=> array(2) { ["fecha"]=> string(8) "03-03-18" ["peso"]=> string(4) "64.0" } }|||array(3) { [0]=> array(2) { ["fecha"]=> string(8) "26-02-18" ["peso"]=> string(4) "67.0" } [1]=> array(2) { ["fecha"]=> string(8) "28-02-18" ["peso"]=> string(4) "66.0" } [2]=> array(2) { ["fecha"]=> string(8) "03-03-18" ["peso"]=> string(4) "64.0" } }
922

Answer

Solution:

You should pass params toRouting as a valid javascript object, changing this line:

Routing.generate('usuarios_dashboard_planes_seguimiento', {planid})

by:

Routing.generate('usuarios_dashboard_planes_seguimiento', {'plan-id': planid})

353

Answer

Solution:

Have a look at the headers of the request. Is the content-type returned by the server, the same in both instances?

You could also try removing dataType: 'json' for a moment to let jquery "Guess"

People are also looking for solutions to the problem: php - CakePHP login from different views

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.