Search code examples
phpajaxxmlhttprequest

Trying to extract information throught a PHP file Undefined


I'm trying to extract the information with a XHR request (AJAX) to a php file (this php file gets the information throught json file with Get request too) so when I try to do console.log(Checker) on the console, it returns Undefined and if I put alert(Checker) it returns [object Object]. How can I solve it?

PHP:

<?php
headers('Content-Type', 'application/json')
$jsonContents = file_get_contents('../data/data.json');
echo $jsonContents
?>

JS:

function start() {

    $.ajax({
        type: 'GET',
        url: 'api/domain/showall.php',
        dataType: 'json',
        success: function(data) {
            alert(data)
            displayTheData(data)
    }
});
}

function displayTheData(data) {
    Checker = data;
    JSON.stringify(Checker)
    console.log(Checker)
    window.Checker = Checker;
}

JSON:

[{"name":"Google","url":"google.es","id":1}]

Solution

  • Here you are strigify data but not store value i any var.

    function displayTheData(data) {
        Checker = data;
        var displayChecker = JSON.stringify(Checker) /// add  displayChecker  
        console.log(displayChecker )    // print it 
        window.Checker = Checker;
    }
    

    There is not displayTheData() function so first call it and pass response params.