我知道这个问题已经被问过一千遍了。我已经浏览了每一个答案,但似乎没有任何效果,或者我没有提出正确的问题。
我正在做一个小型全栈项目。我的后端是在
port:8000
中运行的 Node 和 Express。相关部分(我认为)是这样的:
AppDataSource.initialize()
.then(()=>{
...
const app = express()
app.use(express.json())
app.use(cookieParser())
app.use(cors({
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
origin: ['localhost:3000'],
methods: ['GET', 'POST', 'PUT', 'DELETE']
}))
...
})
前面运行在
localhost:3000
上,是ReactJS并使用axios
。相关代码是这个:
const deleteUser = async (id: number) => {
if (window.confirm('Are you sure you want to delete this record?')){
try {
await axios.delete(`users/${id}`)
setUsers(users.filter((u: UserType) => {
return u.id !== id
}))
}catch(e){
console.log(e)
}
}
}
在 Chrome 中我得到这个:
我可以补充一点,我在 Chrome 中安装了
Access-Control-Allow-Origin
插件并处于活动状态,并且 Postman 应用程序在删除记录方面完全没有问题。
有什么帮助吗?
暂时将您的配置设置为
allowedHeaders: '*',
credentials: true,
origin: '*',
methods: '*'
}));
看看问题是否仍然存在。