如何使用 PHP 启用 CORS

How to enable CORS with PHP

我有一个 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.

我知道是因为我正在尝试从 localhost 访问到 localhost 才出现此问题。

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

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

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