0

I'm starting to work with vuejs and I'm trying to make a http get call without sucess. I've tried several example showing how to do that but I get the following error in my console: this.$http is undefined.

html

<!doctype html>
<html>
    <head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.21/css/uikit.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.21/js/uikit.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.21/js/uikit-icons.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.19.1/vis.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.19.1/vis.min.css" rel="stylesheet" type="text/css"/>


     <script src="https://unpkg.com/vue"></script>


    </head>
    <body>

        <div id="app">
            <p v-bind:title='message'>
                {{ message }}
            <p>
            <button v-on:click='changeView("matrice")'>get data</button>


        </div>
    </body>
         <script type="text/javascript" src="/front/lala_web/matrice.js"></script>
</html>

js

var app = new Vue({
  el: '#app',
  data: {
    message: 'Empty data'
  },
  methods:{
    changeView: function(v){
        this.$http.get('/view/'+v)
            .then(function(resp){
                this.message = resp.data;
            })
            .catch(function(){alert('Error')});
        }
    }
})

I'been able to get a http get call working using the following js, but doing so doesn't change data.message value of vuejs and I get the following error data is not defined.

js

var app = new Vue({
  el: '#app',
  data: {
    message: 'Empty data'
  },
  methods:{
    changeView: function(v){
        var viewUrl = '/view/'

        $.ajax({
           url: viewUrl+v,
           method: 'GET',
           success: function (resp) {
                if (resp.error == false){
                    console.log(resp)
                    data.message = resp.data
                }

           },
           error: function (error) {
               console.log(error)

           }
       });

    }
  }
})
1
  • 2
    this.$http is typically added from a plugin called VueResource Commented May 24, 2017 at 20:06

1 Answer 1

1

You need to reference this.message not data.message. Also, you'll need to save a reference to this outside the scope of the ajax call, since this in the success handler doesn't refer to the Vue instance:

changeView: function(v){
    var viewUrl = '/view/'
    var self = this;

    $.ajax({
       url: viewUrl+v,
       method: 'GET',
       success: function (resp) {
            if (resp.error == false){
                console.log(resp)
                self.message = resp.data
            }
       },
       error: function (error) {
           console.log(error)
       }
   });
}
Sign up to request clarification or add additional context in comments.

2 Comments

@BertEvans yep, I misunderstood what was in his data object. Updated
@ArtFilPortraitArtistetisseu If this solved your problem you should upvote and accept it.

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.