本地主机,因此没有允许访问主机

2023-09-10 14:23:15 作者:萌妹妹哟!

要解决CORS问题,我写的有

To solve CORS issue, I wrote there

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With"); 

在我的服务器上的网站

但我在开发过程中,我看到了这个错误

but during my development I'm seeing this error

The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost' is therefore not allowed access.

我想允许原产地:*能解决一切吗?但为什么它说头包含多个值?

I thought allow-origin: * will solve everything? But why it says header contain multiple values?

推荐答案

这是一个普遍的问题时,意外地使CORS两次。检查以确保您没有启用它在Apache中,或者说,头不被设置两次。作为一个理智检查,你可以尝试删除页眉和添加你为你的回应之前就回来。

This is a common problem when accidently enabling CORS twice. Check to make sure you did not enable it in apache, or that the header is not being set twice. As a sanity check you can try to remove the header and add it right back before you serve out your response.

例如:

header_remove('Access-Control-Allow-Origin');
header('Access-Control-Allow-Origin: *');