L
L
last_round2017-07-23 12:19:20
PHP
last_round, 2017-07-23 12:19:20

How to return an array in Vue?

Hello.
Help if you can: I have a php file that connects to the database and encodes json in a loop and pushes it to an array

$result = mysqli_query($connect, "SELECT * FROM `nodes`");
  $arrayJson = array();
  while( ($record = mysqli_fetch_assoc($result)) ){
    $json = json_encode($record);
    array_push($arrayJson, $json);
  }
  mysqli_close($connect);

On the client, a request is made through Vue-resource to this file, but it returns emptiness, or if you add print_r ($arrayJson) to php, it displays the entire array in one line (I understand that the page content comes in response from the server).
this.$http.get(this.url).then(function(response){
          
  console.log(response);
          
}, function(error){
          
})

Question: How to get an array when sending a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2019-03-19
@ettychel

Try this

function Request($request_text) {
  $zapros_bd = mysqli_query($GLOBALS['link'], $request_text); // Выполнение запроса в БД
  printf(mysqli_error($GLOBALS['link'])); //вывод ошибок касающихся БД
  $x = array(); // Объявление массива в переменной x
  while ($array = mysqli_fetch_assoc($zapros_bd)) {
    $x[] = $array; // передаем в массив каждую строчку, что пришла из БД
  }
  echo json_encode($x, JSON_UNESCAPED_UNICODE); // переводим массив в формат JSON, JSON_UNESCAPED_UNICODE - Не кодировать многобайтовые символы Unicode (по умолчанию они кодируются как \uXXXX)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question