Error parsing json javascript [duplicated]

2

I'm working with php, mysql and jquery. Where I create a json with php that brings a single data, and I'll find it with jquery ajax. If I make a console.log to my data variable brought with ajax it shows me the data like this: [{"folio": "456235"}], but when it parses it shows an empty object. I do not know what I may be doing wrong, this is my code js:

function getFolio(){
            var action3 = 'buscaFolio';

             $.ajax({
                 type: 'GET',
                 data: {action: action3,cod_sec: cod_sec,periodo: periodo,nro_oa: nuevo},
                 url: '../folios.php',
                 //dataType: 'Json',
                 success: function (data) {
                     var parsedData = JSON.parse(data);
                     console.log(parsedData);
                 },
                 error: function () {
                     console.log('Error al buscar folio');
                 }
             });

Any idea what's going on? thanks

php code  Model:

public function getFolio($cod_sec,$nro,$periodo){

        $sql = ' SELECT * FROM ventas WHERE codigoseccion='.$cod_sec.' AND nro='.$nro.' AND periodo='.$periodo.' ';
        $sqlQuery = new  SqlQuery($sql);

        $arr = $this->execute($sqlQuery);$ret = Array();

        foreach ($arr as $t) {
            $f = array(
                'folio'=>$t['folio']
            );
            array_push($ret,$f);
        }
        return(json_encode($ret));
    }

Controller:

$cod_sec = $_GET['cod_sec'];
$nro = $_GET['nro'];
$periodo = $_GET['periodo'];
$objOa3 = new Cargos_controller();
print_r($objOa3->getFolio($cod_sec,$nro,$periodo));
    
asked by daniel2017- 11.10.2016 в 17:27
source

5 answers

3

If the answer that the server is giving you is this [{"folio":"456235"}] that means that it is already an object json and you do not have to do this var parsedData = JSON.parse(data); since it is already an object json . This function JSON.parse(data); what it does is that it converts a string to json but if it is already a json it will return an empty object.

    
answered by 11.10.2016 в 17:37
2

Try with:

parsedData[0]

You are bringing an arrangement with the value you expect.

    
answered by 11.10.2016 в 17:45
2

I do not know how you are handling your data from PHP, we could always try to work with JSON Objects. I consider that it is more orderly to give properties to each arrangement.

from the php

$array = array();
$array['folio'] = "1111";
return json_encode($array);

in the JS, what goes between the parse is the return that PHP gives.

var json = JSON.parse('{"folio":"1111"}');
alert(json.folio);
    
answered by 11.10.2016 в 18:12
0

Replaces print_r($objOa3->getFolio($cod_sec,$nro,$periodo)) with

echo $objOa3->getFolio($cod_sec,$nro,$periodo);
    
answered by 11.10.2016 в 18:05
0

Maybe you just missed the json_encode in the php. You have to make sure that your php file ends with echo json_encode(myArray);

I also suggest adding after that a exit; to be sure that it will not give you anything else in that exit.

If you are doing a p rint_r(myArray) you are sending the object and even if the rta of the ajax understands it is not advisable under any point of view to try to use it that way.

The correct thing is to do the echo json_encode(myArray); and then the JSON.parse as you are doing in your code.

    
answered by 11.10.2016 в 17:41