개발/Javascript

Javascript Fetch 사용법

웹 개발자 권혁진 2022. 7. 12. 00:53
// url-encoded 방식일 경우
let result = await fetch('/foo/bar', {
         headers: {
         	'Content-Type': 'application/x-www-form-urlencoded'
         },
         method : 'POST',
         body: new URLSearchParams({
         	//SOME DATA
         }),
 })
 
 // JSON 방식일 경우
let result = await fetch('/foo/bar', {
         headers: {
         	'Content-Type': 'application/json',
         },
         method : 'POST',
         body: JSON.stringify({
         	//SOME DATA
         }),
 })
 
 csrf 토큰 같은 경우 headers 오브젝트에 넣어주면 됩니다.