• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

283
Vistas
Ajax request from PHP don't return me an Array

I'm trying to return an array from a PHP script in Ajax, however, it seems to not work, it returns me a string rather an Array. I'm using CodeIgniter Framework, there is my .php code :

    public function get_form(){

       $donneesModel = new Mdonnees();
       $result = $donneesModel->getAll();

       $data=array(
         'chartDate' => array(),
         'chartTemp' => array(),
       );

       foreach ($result as $row) {
         $data['chartDate'][] = date("d/m", strtotime($row['date']));
         $data['chartTemp'][] = $row['temperature'];
       }

       print json_encode($data);
     }

And there is my Ajax request :

$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data)  {
console.log(data);
....
....
}

My getAll() function just select all from the database, however i'm only using the temperature & date column.

It should return me an Array like this one, But rather it returns me this.

Can you please help me or give me some clue of what is going on ?

Yours sincerely,.

about 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You need to parse the JSON in Javascript:

$.ajax({
    url:"Cdonnees/get_form",
    method:"GET",
    success:function(data)  {
       //this will print the data
       console.log(JSON.parse(data));
       // save it to a variable and can use it in the rest of the program
       const pareseData = JSON.parse(data);
       ....
       ....
    }
 });

and it should work.

about 3 years ago · Santiago Trujillo Denunciar

0

Try this

$.ajax({
        url:"Cdonnees/get_form",
        method:"GET",
        success:function(data)  {
            $.each(data, function (key, value) {
                  console.log(key)
                  console.log(value)
            })
         }
    });
about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda