Answer the question
In order to leave comments, you need to log in
How to pass data from php to js using axios library?
Good afternoon! Help with advice, please.
There is data that comes from php using JSON
The data is processed by the axios library.
const app = new Vue(
{
el: '#app',
data:
{
corrs: []
},
mounted: function(){
this.getAllCorrs();
},
methods: {
getAllCorrs() {
axios.get("http://url.local/api.php?action=read")
.then(response => {
app.corrs = response.data
})
.catch(error => {
console.log('-----error-------');
console.log(error);
})
}
});
<tr v-for="corr in corrs">
<th>{{corr.id}}</th>
<td>{{corr.name}}</td>
<td>{{corr.fivest}}</td>
<td>{{corr.comment}}</td>
</tr>
Answer the question
In order to leave comments, you need to log in
Replace your code with this
PS And it's better to run not in mounted, but in created hook
main.js
import Vue from 'vue'
import App from './App'
const app = new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
<template>
<div>
<tr v-for="corr in corrs" :key="corr.id">
<th>{{corr.id}}</th>
<td>{{corr.name}}</td>
<td>{{corr.fivest}}</td>
<td>{{corr.comment}}</td>
</tr>
</div>
</template>
<script>
import axios from 'axios'
export default {
data () {
return {
corrs: []
}
},
created () {
this.getAllCorrs()
},
methods: {
getAllCorrs () {
axios.get("http://url.local/api.php?action=read").then(response => {
this.corrs = response.data
}).catch(error => {
console.log('-----error-------')
console.log(error)
})
}
}
}
</script>
<div id="app"></div>
<script src="/js/my.js"></script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question