Next.js Discord

Discord Forum

Error: Unexpected token < in JSON at position 0

Unanswered
Bigeye tuna posted this in #help-forum
Open in Discord
Bigeye tunaOP
why i always getting that error when i try to fetch data in server client? i have no idea what's going on

i use Next js 14.2.3 version, and this is on HomePage page.jsx

13 Replies

Bigeye tunaOP
the fetchConfig func is basically just like this
@Bigeye tuna the fetchConfig func is basically just like this
are you fetching your own api routes or an external backend?
Bigeye tunaOP
external backend
alright. in your fetchConfig, try console.log the url you are trying to fetch
console.log(`${process.env.NEXT_PUBLIC_API_URL}${url}`)

what does it say? does it look correct?
@joulev alright. in your `fetchConfig`, try `console.log` the url you are trying to fetch tsx console.log(`${process.env.NEXT_PUBLIC_API_URL}${url}`) what does it say? does it look correct?
Bigeye tunaOP
yeah, that's look correct. the result is api domain + api url

i'm pretty sure that code was work before
@Bigeye tuna yeah, that's look correct. the result is api domain + api url i'm pretty sure that code was work before
hmm, try this then:

in getHeroData,
const text = await res.text();
console.log(text);
return JSON.parse(text);

what does it log? i think it will log some html instead of json
if it logs html, it means the response is html and you can't .json it – probably the backend is at fault or the url is wrong
high chance this is the 404 page url
Bigeye tunaOP
still got the same error. btw, i can't see console.log message. maybe because it is on server client
@Bigeye tuna still got the same error. btw, i can't see console.log message. maybe because it is on server client
yeah the same error is expected. it should be on the server (the terminal) since this is a server component
@joulev yeah the same error is expected. it should be on the server (the terminal) since this is a server component
Bigeye tunaOP
oh, i see...i check my terminal and this is the result of that console.log message, a lot message like that
yeah right, you see, that's an html response and not a json response so you can't res.json it. now check the html itself and the url you used and see what the mistake is
may have to contact the backend guy to see why it is an html and not a response response