Go through Array of Array json with jquery

0

I have a cakephp function that returns data in json format in the following way

 $response = array($query_license,$query_estate,$result_license);
    if ($this->request->is('ajax')) {
        echo json_encode($response);
        die();
    }

Well to be able to recover the data I do the following

$.ajax({
            type: 'GET',
            async: true,
            cache: false,
            url: license,
            dataType: 'json',
            data:{id_user_license:id_user_license,id_evaluation:id_evaluation},
            success: function (data){
                $.each(data,function(i,s){
                    if(s[0].cantidad_pendiente === 0){
                        $("#message_license").html('<p>Usted no cuenta con licensias disponibles</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>');
                    }else if(s[0].estado === 3){
                        $("#message_license").html('<p>Usted cuenta con '+ s[0].cantidad_pendiente +'</p>');
                        $("#message_consumed").html('<p>Esta evaluacion ya consumio licencia</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>');
                    }else{
                        $("#message_license").html('<p>Usted cuenta con '+ s.indexOf(3).cantidad_pendiente +'</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button> <button type="submit" class="btn btn-danger">Consumir</button>');
                    }
                });
            }
        });

The problem is that when I retrieve the values to show them to the user it shows me undefined and verifying inside the console I see this

console.log(s[0].cantidad_pendiente);
5
undefined

to verify that data is arriving in the data object perform console.log(data);

and this is the result

for some reason it brings me the value and then it shows empty something I'm doing wrong or I have to change thanks for your help

    
asked by Jonathan Cunza 20.01.2017 в 14:58
source

1 answer

0

I think the problem lies in PHP and that you are returning an associative array in JSON without defining it in PHP.

Try to change

This

echo json_encode($response);

Because of this

echo json_encode($response, true);

The argument we add is "assoc", by default it is false unless you set it to true. You can read more about this feature in link .

    
answered by 20.01.2017 / 17:30
source