Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
我在node.js
和express.js
中的后端
import express from 'express';
import bcrypt from 'bcrypt-nodejs';
import cors from 'cors';
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(cors());
const database = { users: [
{
id: '123',
name: 'John',
email: 'john@gmail.com',
password: 'cookies',
entries: 0,
joined: new Date()
},
{
id: '124',
name: 'Tom',
email: 'Tom@gmail.com',
password: 'apple',
entries: 0,
joined: new Date()
}
}
app.get('/', (req, res) =>{
res.send(database.users)
})
app.listen(3002, () => {
console.log('app is running on port 3002');
})
我的前额在React.js
这是一个很大的项目,所以我只会显示导致response.json()
部件出错的部分。当您摆脱json()
时,一切都很好,但是为了让我从后端接收数据,我需要执行.json()
,这会导致错误。如果需要更多的信息,请告诉我
componentDidMount(){
fetch('http://localhost:3000')
.then(response => response.json())
.then(console.log)
}
发布于 2022-09-03 08:48:28
看起来您的后端是在端口3002上公开的,但是您正在获取端口3000,我假设它是您的本地开发UI。DOCTYPE
响应指示您接收的是HTML而不是JSON响应。将获取的端口更改为指向后端,它应该可以工作:
componentDidMount(){
fetch('http://localhost:3002')
.then(response => response.json())
.then(console.log)
}
https://stackoverflow.com/questions/73590839
复制相似问题