Vuejs从PHP/MySQL获取请求不';行不通

Vuejs从PHP/MySQL获取请求不';行不通,php,mysql,vue.js,Php,Mysql,Vue.js,我正在编写代码从MySQL数据库获取数据,并使用Vuejs this.$http.get("api.php?action=read") .then(response => { console.log(response); }, error => { console.log(error); }); 此请求返回所有api.php内容,但不返回数据 下面是api.php文件 <?php r

我正在编写代码从MySQL数据库获取数据,并使用Vuejs

this.$http.get("api.php?action=read")
        .then(response => {
          console.log(response);
        }, error => {
          console.log(error);
        });
此请求返回所有api.php内容,但不返回数据

下面是api.php文件

<?php

  require_once('include/config.php');

  $res = (['error' => false]);

  $action = 'read';

  if (isset($_GET['action'])) {
    $action = $_GET['action'];
  }

  if($action == 'read') {
      $images = array();

      $query = 'SELECT * FROM images ORDER BY id DESC';
      $select_images = mysqli_query($connection, $query);
      while($image = mysqli_fetch_assoc($select_images)) {
        array_push($images, $image);
      }
      echo json_encode($images);
  }

  mysqli_close($connection);
  header("Content-type: application/json");
  echo json_encode($images);
  die();
?>


有人能帮我吗?

请尝试
response.body
获取响应数据:

this.$http.get("api.php?action=read")
        .then(response => {
          console.log(response.body);
        }, error => {
          console.log(error);
        });

尝试
response.body
获取响应数据:

this.$http.get("api.php?action=read")
        .then(response => {
          console.log(response.body);
        }, error => {
          console.log(error);
        });
它起作用了

我补充说

header("Access-Control-Allow-Origin: *");
到API文件

并从localhost/vuejs链接发出get请求, 不像localhost:8080 这就是工作

谢谢

它很管用

我补充说

header("Access-Control-Allow-Origin: *");
到API文件

并从localhost/vuejs链接发出get请求, 不像localhost:8080 这就是工作


谢谢

在php中包含标题。注意:在您返回任何内容之前,应该是第一行

<?php
header("Access-Control-Allow-Origin: *");
注意:尝试使用不同的端口号,因为我无法从运行vueProject的同一8080端口获取api。 这将使用php创建一个api端点。它会听的

现在,当使用axios.get()时,将获得api的响应。我没有尝试过没有axios模块,但我想它应该可以工作

allRecords: function(){
  axios.get('http://localhost:8888/VueAxios/ajaxfile.php').then(response => {
    this.users = response.data
    console.log(response.data)
  })
  .catch(function (error) {
    console.log(error);
  });
}

这个功能对我有用。注意:axios在这种情况下非常方便。希望有帮助。

在php中包含标题。注意:在您返回任何内容之前,应该是第一行

<?php
header("Access-Control-Allow-Origin: *");
注意:尝试使用不同的端口号,因为我无法从运行vueProject的同一8080端口获取api。 这将使用php创建一个api端点。它会听的

现在,当使用axios.get()时,将获得api的响应。我没有尝试过没有axios模块,但我想它应该可以工作

allRecords: function(){
  axios.get('http://localhost:8888/VueAxios/ajaxfile.php').then(response => {
    this.users = response.data
    console.log(response.data)
  })
  .catch(function (error) {
    console.log(error);
  });
}

这个功能对我有用。注意:axios在这种情况下非常方便。希望有帮助。

@MahmoudEl halaq控制台.log(响应)的输出是什么样子的?这张控制台的照片@MahmoudEl halaq控制台.log(响应)的输出是什么样子的?这张控制台的照片