Axios DELETE 方法被 CORS 策略阻止。我错过了什么?

问题描述 投票:0回答:1

我知道这个问题已经被问过一千遍了。我已经浏览了每一个答案,但似乎没有任何效果,或者我没有提出正确的问题。

我正在做一个小型全栈项目。我的后端是在

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 应用程序在删除记录方面完全没有问题。

有什么帮助吗?

node.js google-chrome axios cors crud
1个回答
0
投票

暂时将您的配置设置为

    allowedHeaders: '*',
    credentials: true,
    origin: '*',
    methods: '*'
}));

看看问题是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.