PHP不返回Ajax Call上的数据?

I have this unusual problem in heroku, where I'm loading some session data from a PHP backend using Javascript fetch.

The php file looks like this,

<?php
  session_start();
  header('Content-Type: application/json');

  $data = json_encode($_SESSION['data']);
  print_r($data);

And calls it using a Javascript

fetch('./data.php')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

When I load the data from my browser the output on the console is, parsed json null

But when I load the same data.php on my browser by directly going to it, it prints data data fine on the browser.

What have I done wrong to make it behave in two different ways when calling using ajax not to work and make it work when directly going to the file? Thanks

I have executed your code and got null answer because currently, i don't have any data in session data variable.
So, i guess your session data is not working correctly, try to print the session data first separately in your application then, try to execute this code.