[Vuejs]-How to get url from :href in vue and axios?

0👍

You can use destructuration in ES6 to get targeted element, in your case :

 <button :href="'{% url 'follow' recipe.added_by.id %}'" @click="followUser">Obserwuj</button>

 ////

 methods: {
        followUser({ target }) {
            const url = target.getAttribute('href')
            axios.post(url) 
                .then(response => {
                    console.log(response.data);
                })
                .catch(errors => {
                    if (errors.response.status == 401){
                        window.location = '/login';
                    }
                });
        }
    },

0👍

You cannot send a POST request via URL because the post parameters are not sent via the URL but you can use an alternate solution and instead of using a link to use a button that looks like a link and is in a form: How to make button look like a link? make-button-look-like-a-link.

have a good day, hope you find it helpful.

Leave a comment