ItSolutionStuff.com

Vue JS Ajax Form Submit Example Code

By Hardik Savani • July 5, 2023
Vue.JS

I will share example of how to ajax form submit using api in vue js app. you can easily pass form data with ajax post request in vue.js. we will create simple form and submit form using post ajax request in vue js.

in this example we will use axios for ajax api request and send form data in vue app. you can see bellow full example.

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 follow bellow step by step for ajax request with vue js app.

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 class="container">

<div class="row justify-content-center">

<div class="col-md-8">

<div class="card">

<div class="card-header">Vue Axios Post - ItSolutionStuff.com</div>

<div class="card-body">

<form @submit="formSubmit">

<strong>Name:</strong>

<input type="text" class="form-control" v-model="name">

<strong>Description:</strong>

<textarea class="form-control" v-model="description"></textarea>

<button class="btn btn-success">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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Data Attribute Value in Vue JS?

Read Now →

Vue JS Get Dropdown Selected Value Example

Read Now →

How to Get Current Date and Time in Vue JS?

Read Now →

Laravel Vue Datatables Component Example

Read Now →

How to use datepicker in vuejs?

Read Now →

Vue JS MultiSelect Dropdown Example

Read Now →

Vue Js Sweetalert Modal Notification Example

Read Now →

How to Change Date Format using Moment in Vue JS?

Read Now →

Vue JS Toggle Switch Button Example

Read Now →

Vue Toastr Notifications Example Code

Read Now →

Laravel Vue JS Axios Post Request Example

Read Now →

Laravel Vue JS Infinite Scroll Example with Demo

Read Now →

Laravel Vue JS Pagination Example with Demo

Read Now →

Dynamic Dependent Dropdown using VueJS and PHP

Read Now →