[Vuejs]-Axios request in Vue.js method working when launched from "mounted()" but not when launched from html <form>

3👍

You should add prevent modifier :

<form @submit.prevent="axiosGetToken()">
    <button type="submit">Axios login</button>
</form>

by default the submit event try to reload the page and searching an action from the back-end in order to run it, so the prevent modifier will prevent the action and allows the running of the js event handler.

1👍

As mentioned above, if you use a form with a button of type=”submit”, when pressing that button, it will use the default client behavior and send a request to the form action URL, adding a prevent will stop that behavior.

Although it is a valid answer, I would suggest adding a @click=”axiosGetToken()” on the button.

    <form>
        <button type="button" @click="axiosGetToken()">Axios login</button>
    </form>
👤Eduard

Leave a comment