Why i am getting the error: unexpected token h in JSON at position 0? [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why i am getting the error: unexpected token h in JSON at position 0? [Solved]

So i am making request to this server and getting data from it using the following code but I get the above mentioned error ,so can anybody tell me why is it so and how can I solve it ,also using res.text() works fine,thank you in advance! const fet=require("node-fetch"); fet('http://localhost:9000/') .then(res=>res.json()) .then(body=>console.log(body)); https://code.sololearn.com/c7leBCM7fiHI/?ref=app

15th Aug 2020, 6:20 PM
Abhay
Abhay - avatar
3 Answers
+ 3
because "hello" is not in JSON format maybe it should look like this {"mesage":"hello"}
15th Aug 2020, 6:27 PM
Taste
Taste - avatar
+ 1
change the res.write to json instread of plain string JSON.stringify({message:"hello"}) or '{"mesage":"hello"}'
15th Aug 2020, 7:02 PM
Taste
Taste - avatar
0
Taste so can you pls tell what changes I need to make in both of the codes ?
15th Aug 2020, 6:55 PM
Abhay
Abhay - avatar