如何使用 PHP 启用 CORS

Jos*_*ebi 0 php wamp cors

我有一个 Angular 4 应用程序与驻留在 WAMP(www 文件夹)上的 php 脚本通信,它们都在我的本地计算机上。当我点击脚本时,我在浏览器中记录了此错误。

No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:4200' is therefore not allowed access. 
The response had HTTP status code 404.
Run Code Online (Sandbox Code Playgroud)

我知道这是因为我试图从本地主机访问本地主机,所以出现了这个问题。

我读了一些书,看来我需要从 wamp 服务器修复它。不知道如何在 wamp 上做到这一点。

yBr*_*sky 6

正如所评论的,问题是您需要允许来自脚本的所有来源。

//at the very begining of your php script.
header('Access-Control-Allow-Origin: *'); 
Run Code Online (Sandbox Code Playgroud)