小程序 APP 等都是跨域访问API接口的,浏览器是不支持跨域的,除非特别申明允许其他域访问,下面就给大家分享跨域请求 Apache 服务器配置。

找下面这行,把#去掉,目的是开启apache头信息自定义模块
# LoadModule headers_module modules/mod_headers.so
没有的话直接添加上去(modules 有mod_headers.so模块)
<Directory /www/web/yuming.com/public_html/>
Options FollowSymLinks
AllowOverride All
Require all granted
Header set Access-Control-Allow-Origin *
# 或者:
Header set Access-Control-Allow-Origin http://baidu.com
Header set Access-Control-Allow-Origin https://www.baidu.com
Header set Access-Control-Allow-Origin http://localhost:8080
</Directory>
注意:Header set Access-Control-Allow-Origin http://localhost:8080, https://www.baidu.com, http://baidu.com 这样是错误的。
如果只是一个文件的话,你可以说使用下面的方法:
header('Content-type: application/json');
header('Access-Control-Allow-Origin:*');
这个方法针对于单个 PHP 文件,而不用修改 Apache 的配置文件。

