PHP将PHP数据传递到JS数组中

I have a PHP file which is querying a MySQL database in order to return results, I'm currently saving the relevant result row into a PHP array and I'm then trying to echo out a JS array with the PHP data stored. I do not seem to be able to access the JS variable from my JS files.

PHP:

  while($row = mysqli_fetch_array($query)) {
    $dataArray = array($row['stepNumber']);
    echo '<script>';
    echo 'var dataArray = ' . json_encode($dataArray) . ';';
    echo '</script>';
  }

JS:

$.ajax({
    type: 'POST',
    url: 'queries/dateRangeSelect.php',
    dataType: 'text',
    data: {startDate: startDate, endDate: endDate},
    cache: false,
    success: function(response) {
        console.log(response);
        window.alert(dataArray);
    },

dataArray is being reported as not defined when the window.alert tries to trigger.

EDIT:

  while($row = mysqli_fetch_array($query)) {
    $dataArray = array($row['stepNumber']);
  }
  header('Content-Type: application/json');
  echo $dataArray;

It is no longer printing errors but the console is now not printing anything.

Don't return a <script> tag, you can just return the JSON:

header('Content-Type: application/json');
echo $json;

You probably want to do this AFTER your loop is complete and you have built up the array you wish to return.

UPDATE

Based on your edit, I suggest something more like this:

$dataArray= [];
while($row = mysqli_fetch_array($query)) {
    $dataArray[] = array($row['stepNumber']);
}
header('Content-Type: application/json');
echo json_encode($dataArray);