개발자 삽살개

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 에러 본문

개발하다가

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 에러

개발자 삽살개

Json을 받아와서 콘솔에 출력해보려고 했습니다.

그러나  Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 에러가 발생했습니다.

getApi() {
  fetch('/api')
  .then(res => res.json())
  .then(data => console.log(data))
}

 

해결방법

- JSON 데이터 요청시,Headers Accept 속성에 application/json을 명시해서 JSON을 가지고오겠다라고 알려주는 것입니다.

HTTP Request Headers에 accept를 추가했습니다.


getApi() {
  fetch('/api', {
       headers : {
            'Accept': 'application/json'
       }
  })
  .then(res => res.json())
  .then(data => console.log(data))
}
Comments