vue.js - How do I call a php file with axios?

Solution:

In axios you are referencing a file which will not work

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 makes http requests meaning it needs to be calling a server. You will need to have the PHP file running on some sort of server to be able to call it from 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;
        }
      });
  },

If working with windows, look into WAMP, if working in OSX look into MAMP

These are two simple PHP servers you can configure

Answer

Solution:

Hey I think this is the best solution as it (for me) works locally and when running in production!

this.axios.get(`/thef_file_you_want_to_get/execute.php?, //query-params...
               function (req, res) {
                    // for testing cors...
                    // res.header("Access-Control-Allow-Origin", "*");
               }

              ).then(function (response) {
                console.log(response);
            })

By removing the origin, 'http(s)://localhost:3000' for example, the request will happen to the current domain. 'yourwebsite.com' => https://yourwebsite.com /thef_file_you_want_to_get/execute.php? for example.

Source