[Vuejs]-Vuejs get request from PHP/MySQL doesn't work

1👍

It works,

I added

header("Access-Control-Allow-Origin: *");

to the API file

and make the get request from the localhost/vuejs link,
not like localhost:8080
and it’s work

Thanks

1👍

Include the header in the php. Note: should be the 1st line before you return anything.

<?php
header("Access-Control-Allow-Origin: *");

And try to run the php code 1st using command prompt:

php -S localhost:8888 ajaxfile.php

Note: try to use different port number as for me it is unable to get the api from same 8080 port in which my vueProject is running.
This will create an api end point with your php. Which will listen to http://localhost:8888/projectname/api.php.

Now When used axios.get(http://localhost:8888/projectname/api.php) will get the response for the api.I havent tried without axios module but i guess it should work.

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);
  });
}

This function worked for me. Note: axios is a very handy in this situation. Hope it helps.

0👍

Try response.body to get the response data:

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

Leave a comment