-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path22SubmitDataToAPI.txt
75 lines (65 loc) · 2.27 KB
/
22SubmitDataToAPI.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
43.Submit Form Data to API:
- make a form
- insatll axios (npm i vue-axios axios)
- get from data
- submit data to API
- Test API result
- https://github.com/typicode/json-server
- https://www.npmjs.com/package/vue-axios
- PostComponent.vue file:
<template>
<div>
<form @submit="submitData" method="post"> -> post is a method
<input type="text" placeholder="Enter title" name="title" v-model="post.title" /><br><br>
<input type="text" placeholder="Enter author name" name="author" v-model="post.author" /><br><br>
<button type="submit">Post</button>
</form>
</div>
</template>
<script>
import Vue from 'vue';
import axios from 'axios';
import VueAxios from 'vue-axios';
Vue.use(VueAxios, axios);
export default {
name: 'PostComponent',
data() {
return {
post:{
title:null,
author:null
}
}
},
methods: {
submitData(e)
{
this.axios.post('http://localhost:3000/posts',this.post).then((result)=>{
console.warn("result",result);
})
e.preventDefault();
}
}
}
</script>
- http://localhost:3000/posts ki fake API create kari hai.
- 20JsonServer.text me dekhe video ki link hai -> How to make fake api using Json Server
- App.vue file:
<template>
<div id="app">
<h1>Submit Form Data to API</h1>
<PostComponent />
</div>
</template>
<script>
import PostComponent from "./components/PostComponent.vue";
export default {
name: "App",
components: {
PostComponent
}
};
</script>
- Create an Dummy API after that, run the page on browser and fill the details like author and title.
- After that, click post button and see the result on console page me Network hai usme.
- Uske Niche post ki api create ho jayengi and Preview me new author and title show karega.