I have created an api using Laravel 5.5 and tested it in Postman. So, in Postman it's working correctly, BUT when I try to connect it from my frontend I'm getting some errors. I'm using Vue.js on frontend.
Here's the request from frontend using axios:
<script>
import axios from 'axios';
export default {
data () {
return {
posts: {}
}
},
created() {
axios({
method: 'get',
url: 'http://localhost:8000/api/users',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Credentials': true,
'Content-Type': 'text/html; charset=utf-8'
}
}).then(response => {
this.posts = response.data;
console.log(this.posts[0].body);
}).catch(error => {
console.error(error.message);
});
}
}
</script>
And the api route:
Route::get('users', 'UserController@index');
An API is running on localhost:8000 via php artisan serve command.
And Vue.js app is running on localhost:8000 via yarn run dev command.
Is there any workaround?
