1

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,.

1
  • 2
    please don't post images of code, error messages or other like variables, etc. Add them as code-formatted text instead, since none of us can copy, paste and run an image. For more on this, please see why we Discourage screenshots of code and/or errors Commented Jan 20, 2022 at 20:18

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello, thanks for your answer. It appers well in the console.log, however now how can i parse it for all the script? Thanks :)
@juststartedcodeigniter what do you mean by parsing for the whole script?
As you can see here link : my results don't show up on my graph and when i put a console.log(data) before and after the parse, the result in my console is still the same : link as you can see here too :-)
@juststartedcodeigniter It was a good practice if you share all of your code in the success function. whatever I update the answer that might be fixed your problem. just use the pareseData in the rest of the codes.
0

Try this

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

1 Comment

or simply: console.dir(data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.