티스토리 뷰
ajax 요청 방법 두가지
- axios 라이브러리 사용
- 기본 fetch 함수 사용
> axios 라이브러리 사용
- 라이브러리 설치하기
npm install axios
- 사용하기
// post 요청
axios.post('URL', {name: 'kim'})
.then((결과) => {
console.log(결과)
})
.catch((에러) => {
console.log(에러)
});
// get 요청
axios.get('URL')
.then((결과) => {
console.log(결과)
})
.catch((에러) => {
console.log(에러)
});
ex) 더보기 클릭시 데이터 받아오기
<template>
<button @click="more">더보기</button>
</template>
<script>
import postData from './data/postData';
import axios from 'axios';
export default {
name: 'App',
data() {
return {
postData: postData,
moreCount: 0,
};
},
methods: {
more() {
axios
.get(`https://codingapple1.github.io/vue/more${this.moreCount}.json`)
.then((res) => {
console.log(res);
this.postData.push(res.data);
this.moreCount++;
})
.catch((err) => {
console.log(err);
});
},
},
components: { Container },
};
</script>'Library | Framework > Vue JS' 카테고리의 다른 글
| [Vue.js] 17. slot (0) | 2023.08.07 |
|---|---|
| [Vue.js] 16. 서버없이 이미지 업로드 (0) | 2023.08.06 |
| [Vue.js] 14-2. 라우터 hash mode, Navigation guards (0) | 2023.08.06 |
| [Vue.js] 14. vue-router, URL parameter (0) | 2023.08.05 |
| [Vue.js] 13. Vue의 lifecycle (0) | 2023.08.05 |
댓글
