N
N
Nickita2019-01-15 10:51:11
HTML
Nickita, 2019-01-15 10:51:11

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

They get into the getAllCorrs() method, but they are displayed in the console only 1 time, then the console is empty.
And nothing is displayed in html either (It seems that I am doing everything right, why is that?
<tr v-for="corr in corrs">
    <th>{{corr.id}}</th>
    <td>{{corr.name}}</td>
    <td>{{corr.fivest}}</td>
    <td>{{corr.comment}}</td>
</tr>

Thanks to all! The problem was on the php side

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dima Pautov, 2019-01-15
@bootd

Replace your code with this
PS And it's better to run not in mounted, but in created hook

A
Alexander Shapoval, 2019-01-15
@sanek_os9

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>

A
Alexander, 2019-01-15
@alexmixaylov

check the context if you lost it?

getAllCorrs() {
                     // добавляем 
                    const self = this;
                    axios.get("http://url.local/api.php?action=read")
                    .then(response => {
                        //app.corrs = response.data
                       // и здесь обращаемся не к this а к self
                          self.corrs = response.data
                    })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question