-1

I'm trying to create a laravel app with a Vue.js to call a controller response, I've tried it with ajax alone and it works, but with Vue the GET method seems not working, there are also no error code for me to debug.

here is my Vue.js

<template>
<div>
    <ul class="list-group list-group-flush">
        <li class="list-group-item"  v-for="unregboard in unregboards" :key="unregboard.boardId"> 
            {{ unregboard.boardName }}  
        </li>
    </ul>
</div>
</template>

<script>
export default {

    data(){
        return{
            unregboards:[]
        }
    } , created: function(){
        this.fetchUnreg()
    }, methods:{
        fetchUnreg: function () {
            console.log("test"); //this works
            this.$http.get('/registerlist', function(resp){
                this.unregboards = resp.unRegBoards;
                console.log(resp.unRegBoards);
                console.log('test'); // This doesn't
            }).bind(this);
        }
    } 

}
</script>

<style>

</style>

UPDATE

I have fixed something and the code, I had my hopes up, because unlike before I was able to log my json response, check my code below:

<template>
    <div>
        <ul class="list-group list-group-flush">
            <li class="list-group-item"  v-for="unregboard in unregboards" :key="unregboard.boardId"> 
                {{ unregboard["boardName"] }}  
            </li>

        </ul>
    </div>
</template>

<script>
export default {

    data(){
        return{
            unregboards:[]
        }
    } , created: function(){
        this.fetchUnreg()
    }, methods:{
        fetchUnreg: function () {
            console.log("test");
            $.get('/registerlist', function(resp){
                this.unregboards = resp.unRegBoards;
                console.log(resp.unRegBoards);
            });
        }
    } 

}
</script>

<style>

</style>

The problem is on the template, I does not return anything on the "unregboards" Vue.js's For loop

Additional Info this is the json file that's being returned:

{
   "regBoards":[
      {
         "boardName":"Client 1",
         "boardId":"594a0ec09ad35eba4434142r",
         "organization":"Dream Inc."
      },
      {
         "boardName":"Welcome Board",
         "boardId":"58d311eeace6df3821107648",
         "organization":null
      }
   ],
   "unRegBoards":[
      {
         "boardName":"Client 2",
         "boardId":"5935fc808823fdbe2875d63g",
         "organization":"Dream Inc."
      },
      {
         "boardName":"Client 3",
         "boardId":"59190bf0b27a6f308820rh5u",
         "organization":"Dream Inc."
      },
      {
         "boardName":"Client 4",
         "boardId":"58a5006e9985e088628f634g",
         "organization":"Dream Inc."
      },

   ]
}
5
  • Are you using webpack/browserify ? Commented Nov 28, 2017 at 3:26
  • stackoverflow.com/questions/42387414/… Commented Nov 28, 2017 at 3:28
  • It's not the same, I've already installed vue-resources, As I said it returns no error, as you can see in the code that I've attached I've tried two console.log, the other one works the other doesn't. Commented Nov 28, 2017 at 3:32
  • 1
    try like this this.$http.get('/registerlist').then( function (response) { }, function (error) { }); Commented Nov 28, 2017 at 3:39
  • I've already tried that still doesn't work Commented Nov 28, 2017 at 3:42

2 Answers 2

1

According to the vue-resource documentation, there is no second callback argument passed to the .get() method, only options.

Assuming the response body is JSON, you also should be parsing the response using the .json() method. Right now, you're assigning undefined to your data property.

fetchUnreg: function () {
  this.$http.get('/registerlist').then(resp => resp.json()).then(data => {
    this.unregboards = data
    console.log(this.unRegBoards)
    console.log('test')
  })
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this,

this.$http.get('/registerlist').then(
    response => {
        this.unregboards = response.data
    }
);

3 Comments

what happen when you log response.body ?
Nothing happens
Instead of using function syntax, use arrow functions and try i have updated code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.