我有一个简单的PHP服务器运行在我的本地。我从前端发送请求到PHP服务器。但我得到CORS错误,即使'header(' Access-Control-Allow-Origin:* “)"出现在PHP文件中。
前端运行在8080端口,PHP服务器运行在3000端口。
下面是我得到的错误
Access to XMLHttpRequest at 'http://localhost:3000/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这些行位于PHP文件的顶部
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization');
知道为什么吗?
2条答案
按热度按时间5rgfhyps1#
如this中所述,对于
Access-Control-Allow-Credentials: true
头文件,您不能传递Access-Control-Allow-Origin: *
,它必须是调用API的特定前端域,而不是通配符。ttygqcqt2#
标题
你好,首先IM新开发抱歉之前.更多帮助,请阅读链接底部,思想不要使用“*”,如果您的服务器试图设置cookie,你使用withCredentials = true
HTTP
更多信息请阅读http http-refrence