Vueapp AJAX阵列不起作用

Hey everyone I have a vueapp that makes an ajax call that looks like this:

$.ajax({
  url: 'addDisplayBackend.php',
  dataType: 'json',
  type: 'post',
  contentType: 'application/json',
  dataType: 'json',
  data: jsonString,
    error: function(data){
      alert('error');
    console.log(data);
    },
  success: function(data){
    console.log(data);
      alert('success');
      this.nameexists=data.nameexists;
      if(this.nameexists==false){
    console.log(data);
    this.displays=data.displays;
    this.hideAddDisplayForm();
    }
  }.bind(this)
});

on the addDisplay backend page I do a number of things but I do I select statement that looks like this:

$selectdisplaysquery="SELECT * FROM display WHERE userId='$userid'";
$displaysresult=mysqli_query($mysqli, $selectdisplaysquery);
while($row=mysqli_fetch_assoc($displaysresult)){
 $data['displays']=$row;
}
echo json_encode($data);

Why doesn't this work? Why cant I have $data['displays'] return an array of all the objects in the database? For some odd reason I just get one row. Any help would be awesome

keep the vueapp the same but change the backend page to be

$selectdisplaysquery="SELECT * FROM display WHERE userId='$userid'";
    $displaysresult=mysqli_query($mysqli, $selectdisplaysquery);
    foreach($displaysresult as $display){
        $displaysarray[]=$display;
    }
    $data['displays']=$displaysarray;

For some odd reason I just get one row.

Seems like your problem is here:

$data['displays']=$row;

The loop always replaces the displays key with the $row value, so you'll end up with only one value after all.

A solution would be the following:

$data['displays'][] = $row;

This way you'll add (push) to the end of the array.

Make sure to create the array first tho, like so:

$data['displays'] = [];
$selectdisplaysquery="SELECT * FROM display WHERE userId='$userid'";
$displaysresult=mysqli_query($mysqli, $selectdisplaysquery);
while($row=mysqli_fetch_assoc($displaysresult)){
 $data['displays'][] = $row;
}
echo json_encode($data);

You can use mysqli_fetch_all() function too.

$selectdisplaysquery="SELECT * FROM display WHERE userId='$userid'";

$displaysresult=mysqli_query($mysqli, $selectdisplaysquery);


$data = mysqli_fetch_all($displaysresult,MYSQLI_ASSOC);

echo json_encode($data);