0

So I have this php class where i have a function that get users from a PSQL database but the AJAX keep loging empty array in the console:

public function getUsers(){
        $query = pg_query(self::$DBH, "select id, name, email, admin from users order by name;");
        $result = array();
        $i = 0;
        while ($row = pg_fetch_row($query)) {
            $result[$i] = $row;
            $i++;
        }
        return $result;
    }

I use a phphandler file to call the function from ajax :

<?php
    include_once $_SERVER['DOCUMENT_ROOT'].'/bdd.php';
    require_once 'modele_backend.php';
    $module = new Modele_Backend();
    echo json_encode($module -> getUsers());



?>

and finaly there is the AJAX call

$(document).ready(function(){
                $("#user_email").on("input", function(){
                    // Print entered value in a div box
                    $.ajax({
                        url: 'modules/mod_backend/backendHandler.php',
                        type: 'post',
                        dataType: 'json',
                        success: function(response) { console.log(response); }
                    });
                });
            });

The problem is that js keep showing empty array in the console.

The json_encode works fine as json_last_error = 0.

I Tried replacing the return of my getUsers() function by

echo json_encode($result);

to test if the function manage to encode my array and it did show up like a JSON on the page so this is not a encoding of my array problem. Still when AJAX get the result of the json_encode function it display an empty array.

Thanks for any help !

Necro.

4
  • What's exactly the output of console.log(response)? [], undefined, some kind of object...? Commented Oct 27, 2019 at 14:03
  • Checking json_last_error doesn't tell you how many rows are returned from the query Commented Oct 27, 2019 at 14:05
  • Yes the console output [] Commented Oct 27, 2019 at 14:07
  • i know, json_last_error tells you if json encode went well. But the query do return rows, i tested it out of ajax multiple times. Commented Oct 27, 2019 at 14:08

2 Answers 2

0

Solution 1 You have to set content type of header in your php file before echo

header('Content-type: application/json');

Solution 2 change dataType in your ajax code to html or remove it to return default dataType (default: Intelligent Guess (xml, json, script, or html))

and then convert returned string to json using javascript JSON.parse() method .

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

Comments

0

It turned ou the problem was not json_encode at all, it was a problem with my static DB class wich I was includong twice with the AJAX call.

Thanks anyway for the support

Comments

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.