0

I am trying to post some data usng this.$http.post and I could not figure out how to pass in the data through the route api..

new Vue({

    el: '#root',

    data: {
        newName: '',
        nameList: []
    },

    methods: {
        addName(){
            this.nameList = this.nameList.concat(this.newName);
            var name = this.newName;
            this.newName = '';

            this.$http.post('/api/name', {name: name}).then((response) => {
                console.log(response.message);
            });
       }
    },

    mounted(){
        this.$http.get('/api/name').then((response) => {
            this.nameList= this.nameList.concat(JSON.parse(response.body));
            console.log(this.nameList);
        });
    }


});

1 Answer 1

2

It is not very clear, what is the exact issue, here, what exact API you are trying to hit.

If you are trying to hit: /api/name/:someName, you can do following

this.$http.post('/api/name/'+name ).then((response) => {
   console.log(response.message);
});

If you are trying to hit: /api/:someName with payload, you can do following

this.$http.post('/api/' + name, {name: name}).then((response) => {
   console.log(response.message);
});

Let me know if it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

The first answer helped. Thank you

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.