I was making a site for a personal project when I ran into an error. The client requests for the list of directories and the server returns a JSON file back to the client but the client brings up the error 'syntaxerror: unexpected end of input'. I checked the server output and it says it sent the file successfully, and when I accessed the server through the web manually, I got a valid JSON output (I checked it online). Is this a problem with the client?
server code:
app.get('/votes', (req, res) => {
fs.readFile(path.resolve(__dirname, 'testing.json'), (err, data) => {
if (err) {
res.status(500).send('Error reading file');
} else {
res.status(200).json(JSON.parse(data));
}
});
});
client code:
async function dataFetch() {
await fetch('https://serverIP/votes', {
method: 'GET',
mode: "no-cors",
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
console.log(`${data} <-- data`);
jsonFile = data;
})
.catch(error => {
alert("an error has occured: "+error)
console.log(`Error: ${error}`);
}
I tried using other methods of fetching, but none were successful.