axios call from vue component code example
Example 1: axios post data vue js
<template>
<form class="" method="post" @submit.prevent="postNow">
<input type="text" name="" value="" v-model="name">
<button type="submit" name="button">Submit</button>
</form>
</template>
export default {
name: 'formPost',
data() {
return {
name: '',
show: false,
};
},
methods: {
postNow() {
axios.post('http://localhost:3030/api/new/post', {
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
body: this.name,
});
},
components: {
Headers,
Footers,
},
};
Example 2: how to use axios in vue
npm install axios
import axios from 'axios'
test () {
axios.post('URL')
.then(function (response) {
alert (response.data);
})
.catch(function (error) {
alert(error);
});
}