post ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์ ์ก
#ajax ํต์ ์ํ lib ์ค์น
npm i axios
App.vue
<template>
<!-- v-on:submit.prevent(๊ธฐ๋ณธ๋์ ๋ง์) -->
<form v-on:submit.prevent="submitForm">
<div>
<label for="username">id: </label>
<!-- v-model์ username๊ณผ ์ฐ๊ฒฐ(๋ฐ์ดํฐ ๋ณ๊ฒฝ ์ ์ธ์คํด์ค์์ ์ฌ์ฉ ๊ฐ๋ฅ) -->
<input v-model="username" id="username" type="text">
</div>
<div>
<label for="password">password: </label>
<!-- v-model์ password์ ์ฐ๊ฒฐ -->
<input v-model="password" id="password" type="password">
</div>
<!-- ํด๋ฆญ์ด๋ฒคํธ๋ก save ์ ์ -->
<button @click="save" type="submit">login</button>
</form>
</template>
<script>
//axios import
import axios from 'axios';
export default {
data: function() {
return {
//v-model๋ก ์ฐ๊ฒฐํ ๋ฐ์ดํฐ
username: '',
password: ''
}
},
methods: {
//submit ์ ์คํ๋๋ ํจ์
submitForm: function() {
// e.preventDefault(); -> v-on:submit.prevent๊ฐ ์ด ์ญํ
console.log(this.username + "\n" + this.password);
//post๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์ ์กํ ์๋ฒ
const url = 'https://jsonplaceholder.typicode.com/users';
//์๋ฒ๋ก ์ ์กํ ๋ฐ์ดํฐ(์
๋ ฅ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ด๊ณณ์ ๋ฐ์ธ๋ฉ)
const data = {
// document.getElementById('username') or $("#username").val() ์ฌ์ฉ ํ์ X
username: this.username,
password: this.password
}
//ajax ํต์
axios.post(url, data)
//์ฑ๊ณต
.then(function(response){
console.log(response);
})
//์คํจ
.catch(function(error){
console.log(error);
});
}
}
}
</script>
<style>
</style>
get๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์กฐํ
<template>
<div>
<app-header></app-header>
<button type="button" @click="getList">์กฐํ</button>
<app-footer></app-footer>
</div>
</template>
<script>
import AppHeader from './components/AppHeader';
import AppFooter from './components/AppFooter';
import axios from 'axios';
export default {
components: {
'app-header': AppHeader,
'app-footer': AppFooter,
},
methods: {
getList: function() {
const url = 'http://localhost:9090/api/board';
axios.get(url)
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
}
}
}
</script>
<style>
</style>
queryString(?empId=dev&empNm=๋ก๋ค์ฃผ&empEmail=dev@naver.com) ์ถ๊ฐ ์ ์๋์ฒ๋ผ ์ฌ์ฉ
const data = {
empId: this.empId,
empNm: this.empNm,
empEmail: this.empEmail
}
//2๋ฒ ์งธ ์ธ์๊ฐ์ ์์์ ๋ง๋ object ์ถ๊ฐ
axios.get(url, { data })
reference: https://www.inflearn.com/course/lecture?courseSlug=age-of-vuejs&unitId=21471&tab=curriculum
ํ์ต ํ์ด์ง
www.inflearn.com
'IT > development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vue.js] vue.js์ ๋ ๋๋ง ๋ฐฉ์๊ณผ ๊ธฐ์กด ๋ฐฉ์์ ์ฐจ์ด์ (feat. ajax) (8) | 2023.07.30 |
---|---|
[Vue.js] watch vs computed(๋ฐ์ดํฐ ๋ณํ ๊ฐ์ง) (0) | 2023.07.30 |
[Vue.js] ์ฑ๊ธ ํ์ผ ์ปดํฌ๋ํธ์์ event emit ์์ฑ ์ด์ฉ (0) | 2023.07.29 |
[Vue.js] ์ฑ๊ธ ํ์ผ ์ปดํฌ๋ํธ์์ props ์์ฑ ์ด์ฉ (0) | 2023.07.29 |
[Vue.js] ์ฑ๊ธ ํ์ผ ์ปดํฌ๋ํธ์์ ์ปดํฌ๋ํธ ์ฌ์ฉ (0) | 2023.07.29 |
๋๊ธ