Rikard Olsson Rikard Olsson - 3 months ago 29
HTTP Question

Http callback is never called, Vue Laravel

Got this in a Vue component (Users.vue). The fetchUsers-function is called but the callback-function is NEVER called. I've tried to attach .error()-func but I get an error of

this.$http.get(...).error is not a function
.

<template>
<div class="panel panel-default">
<div class="panel-heading">Användare</div>

<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users">
<td>{{ id }}</td>
<td>{{ name }}</td>
<td>{{ email }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>

<script>
export default {

name: "Users",

ready() {
console.log('Component ready.')

this.fetchUsers();
},

methods: {
fetchUsers: function() {
console.log("Fetching users...");

this.$http.get('/api/user', (data) => {
this.$set("users", data);
});
}
}
}
</script>


I use Laravel's framework that ships with Vue.js. Don't know how it all works but I get the component showing up in the web browser and I get output in the console from this code.

Answer

I believe that you need to add vue-resources, try adding

<script src="https://cdn.jsdelivr.net/vue.resource/0.9.3/vue-resource.min.js"></script>

this.$http.get('/api/user', (data) => {
    this.$set("users", data);
}, (error) => {
    console.log(error)//check errors
});

or try this

this.$http.get('/api/user').then((data) => {
    this.$set("users", data);
}, (error) => {
    console.log(error)//check errors
});