Front-end/React\RN
리액트 Fetch API 와 axios 를 통해 서버와 통신하기
javapp 자바앱
2022. 6. 16. 00:00
728x90
Fetch API 와 axios 를 통해 서버와 통신하기
fetch
const loadContent= () =>{
const url = 'api/todolist'
const options={
method:"GET"
}
fetch(url,options)
.then((resp)=>{
return resp.json()
})
.then( data => {
console.log(JSON.stringify(data))
setListContent(data)
})
}
axios
- 구형브라우저 지원
- 요청 중단 가능
- 응답 시간 초과 설정 가능
- CSRF 보호 기능 내장
- JSON 데이터 자동변환
- Node.js 에서 사용
npm install axios
// api/todoinsert : 서버 url
const handleSubmit=(event)=>{
event.preventDefault(); //포커스 기능 방지
let data={
//json
subject:inputs.subject,
name:inputs.name,
summary:inputs.summary
}
console.log(data)
axios.post('/api/todoinsert',
JSON.stringify(data),{
headers: {
"Content-Type": `application/json`,
},
}).then((response)=>{
console.log(response)
alert('등록완료')
})
}
Axios를 사용하여 HTTP요청하기
Axios소개Axios는 HTTP통신을 하는데 매우 인기있는 Javascript라이브러리입니다. Axios는 브라우저와 Node.js 플랫폼에서 모두 사용할 수 있습니다. 또한 IE8이상을 포함한 모든 최신 브라우저를 지원합니
tuhbm.github.io