1

I am trying to fetch Data form OpenWeather API.According to the docs the response is JSON.However I am getting an error of :

SyntaxError: Unexpected token < in JSON at position 0

useEffect(() => {
fetch(`api.openweathermap.org/data/2.5/weather?q=${CITY_NAME}&appid=${API_KEY}`)
.then(
  (res)=> res.json()
  .then((data)=>{
    console.log(data)
  })
  .catch((error)=>{
    console.log(error)
  })
)
}, [])

I also have a status code of 304: Not Modified when I looked through the developers tool.

There were few question which matched my problem but none of them solve my problem like: Fetching JSON returns error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 and

React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

1
  • @yudhiesh Yes!! It is valid Commented Jun 21, 2021 at 15:18

1 Answer 1

1

The path to the API is incorrect and should be http://api.openweathermap.org/data/2.5/weather?q=${CITY_NAME}&appid=${API_KEY} instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.