Vue Axios Post Request Example
In this post we will give you information about Vue Axios Post Request Example. Hear we will give you detail about Vue Axios Post Request ExampleAnd how to use it also give you demo for it if it is necessary.
In this post, we will lean how to send http request using axios in vue js. we will send post request with parameter as array or form data in vue cli npm app. here will be simple example of axios post request in vue js app from scratch.
we always choose axios to call api in vue js application.
axios is a http client library. axios provide to send get, post, put, delete request with parameter, formdata, headers, string, image, multipart/form-data etc. axios is a awesome library for http requests.
in this example, we will create simple form with two input fields in vue js app. then we will form submit request by using axios http post request with following input parameter. You can also understand clear to how to send http post request.
So, let’s see bellow example step. Make sure you need to create one POST request api for this example.
Step 1: Create Vue JS App
In this step, we need to create vue cli app using bellow command:
vue create my-app
Step 2: Install vue-axios package
Here we need to install vue-axios npm package for send post request using axios.
npm install --save axios vue-axios
Step 3: Use vue-js-toggle-button
We need to use vue-axios package in main.js file of vue js app.
src/main.js
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
Step 4: Update HelloWorld Component
Here, we will create HelloWorld.vue component with following code.
src/components/HelloWorld.vue
<template>
<div >
<div >
<div >
<div >
<div >Vue Axios Post - ItSolutionStuff.com</div>
<div >
<form @submit="formSubmit">
<strong>Name:</strong>
<input type="text" v-model="name">
<strong>Description:</strong>
<textarea v-model="description"></textarea>
<button >Submit</button>
</form>
<strong>Output:</strong>
<pre>
{{output}}
</pre>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
},
data() {
return {
name: '',
description: '',
output: ''
};
},
methods: {
formSubmit(e) {
e.preventDefault();
let currentObj = this;
this.axios.post('http://localhost:8000/yourPostApi', {
name: this.name,
description: this.description
})
.then(function (response) {
currentObj.output = response.data;
})
.catch(function (error) {
currentObj.output = error;
});
}
}
}
</script>
Now you can run vue app by using following command:
npm run serve
Get more info from here: https://www.npmjs.com/package/vue-axios.
I hope it can help you…
Hope this code and post will helped you for implement Vue Axios Post Request Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs