1. FormData객체를 이용하는 경우
- Axios는 HTML form 태그에 담긴 데이터(이미지, 동영상 파일 등)를 Ajax요청으로 보내는 경우에 이용한다.
2. FormData 메서드
1) append : 데이터 하나씩 추가
2) has : 데이터 존재여부 확인
3) get : 데이터 조회
4) getAll : 데이터 모두 조회
5) delete : 데이터 삭제
6) set : 데이터 수정
const formData = new FormData()
formData.append('name', 'zerocho')
formData.has('item')
formData.get('item')
formData.getAll('item')
formData.delete('test')
formData.set('item', 'apple')
3. FormData POST 요청으로 보내기
- Axios의 data자리에 formData를 넣어 보낸다.
(async(){
try{
const formData = new FormData() //formData생성자
formData.append('name', 'zerocho') //formData에 'name': 'zerocho'
formData.append('birth', '1994') //formData에 'birth': '1994' 추가
const result = await axios.post('주소', formData) //axios를 이용해 주소로 formData를 보내 요청한 결과값을 result에 담는다.
console.log(result)
console.log(result.data)
}catch(error){
console.error(error)
}
})( )
728x90
'Node.js' 카테고리의 다른 글
익스프레스 프레임워크 사용하 (0) | 2023.08.13 |
---|---|
간단한 서버 구현하기 (0) | 2023.08.13 |
브라우저-서버의 요청-응답 (0) | 2023.06.29 |
널병합 연산자 / 옵셔널 체이닝 (0) | 2023.06.29 |
<Map / Set> (0) | 2023.06.29 |