【问题标题】:How do I call a php file with axios?如何用 axios 调用 php 文件?
【发布时间】:2021-02-05 13:09:09
【问题描述】:

我正在使用 vue.js 和 php 进行 CRUD。我安装了 axios,我相信它正在工作。就是不知道怎么调用连接数据库的php文件并创建、读取...

文件夹结构:

我的脚本摘录:

<script>
 import axios from 'axios';
 export default {
 name: "HelloWorld",
 data() {
   return{
     showAddModal: false,
     showEditModal: false,
     showDeleteModal: false,
     errorMsg: "",
     successMsg: "",
     projects: [],
     newProject: {
       title: "",
     },
     currentProject: {},
  }
},
created: function () {
  this.getAllProjects();
},
methods: {
  getAllProjects: function () {
    axios
      .get("../php/action.php?action=read")
      .then(function (response) {
        if (response.data.error) {
          this.errorMsg = response.data.message;
        } else {
          this.projects = response.data.projects;
        }
      });
  },

【问题讨论】:

  • 您需要运行某种 PHP 服务器才能调用它。您当前运行 PHP 文件的设置是什么?
  • 是真的,有这个细节。我该如何解决这个问题? xampp 会起作用吗?
  • “通话”是什么意思?为什么不使用 AJAX 请求?

标签: php vue.js


【解决方案1】:

在 axios 中,您引用了一个不起作用的文件

methods: {
  getAllProjects: function () {
    axios
      .get("../php/action.php?action=read") // This will not work
      .then(function (response) {
        if (response.data.error) {
          this.errorMsg = response.data.message;
        } else {
          this.projects = response.data.projects;
        }
      });
  },

Axios 发出 http 请求意味着它需要调用服务器。您需要在某种服务器上运行 PHP 文件才能从 axios 调用它

methods: {
  getAllProjects: function () {
    axios
      .get("http://localhost:8000/some-url") // This will work
      .then(function (response) {
        if (response.data.error) {
          this.errorMsg = response.data.message;
        } else {
          this.projects = response.data.projects;
        }
      });
  },

如果使用 Windows,请查看 WAMP,如果使用 OSX,请查看 MAMP

这是您可以配置的两个简单的 PHP 服务器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2020-05-24
    • 2019-12-29
    相关资源
    最近更新 更多