I
I
Igor Che2017-03-10 07:49:25
JavaScript
Igor Che, 2017-03-10 07:49:25

How to make Ajax requests in vue.js?

I'm trying to figure out how to bind Ajax to vue. The search does not help, the documentation of the framework is very good, but for some reason it also bypasses the question of how to actually deliver data to the vue application.
I write like this:

new Vue({
        delimiters: ['{*', '*}'],
        el: '#app',
        data: {
            message: 'Hello Vue.js!',
            values: []
      },
        created: function() {
            this.$http.get('/inspector/vue').then(
            function (response) { // Success.
                console.log(response.data);
                this.values = response.data;
                },
            function (response) { // Error.
                    console.log('An error occurred.');
                }
            );
        }
    })

I get an error: "TypeError: this.$http is undefined".
How is it customary to make such requests in vue? Can normal ajax be added to created?
I'm going to load the data when the page loads, and then make a request to update the data at a certain interval.
Do I understand correctly that you need to create a method that loads data and call it in created, and then call the same method at a certain interval?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ivan Bogachev, 2017-03-10
@chewarer

How is it customary to make such requests in vue?

Now it is fashionable to use fetch - codepen (just need to substitute polyfill for better browser support).
It would make sense, yes.

H
he he, 2017-03-10
@tosha_lol_daaa

Try using axios :)

axios.get('/url').then((response) => this.data = response.data.response)
.catch((error) => console.log(error.response.data));

Ps: import axios from 'axios'; for use

M
mina_rina, 2017-07-26
@mina_rina

Here it is described in detail.

Z
zorca, 2017-03-10
@zorca

https://laracasts.com/series/learn-vue-2-step-by-s...

G
Gran Torino, 2018-08-03
@adrin13

install and use vue-resource

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question