JQuery how to read json cache?

Viewed 1029

According to this answer, I set cache for my json data:

session_start();
if(isset($_SESSION['dataCache'])) {
    echo json_encode($_SESSION['dataCache']);
} else {
    $file = 'data.json';
    if (!is_file($file) || !is_readable($file)) {
        die("File not accessible.");
    }
    $contents = file_get_contents($file);
    $_SESSION['dataCache'] = json_decode($contents, true);
    echo $contents;
}

Now I want to read this cached from javascript by this code:

if(localStorage.getItem("dataCache")) {
    data = JSON.parse(localStorage.getItem("dataCache"));

But, the problem is localStorage.getItem("dataCache") returns null.

How do I read cache that created in PHP session from JavaScript?

3 Answers
Related