我将下面的代码,放在Codeigniter
项目中的index.php
中的
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Accept, Access-Control-Request-Method, Referer, Content-Type, Content-Length, Accept-Encoding');
//运行请求的方法
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
//当遇到 OPTIONS 请求时,不执行,直接接收
if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
die();
}
Access-Control-Allow-Origin
可以是特定的地址,如 .http://localhost:8001 只有 http://localhost:8001 能够访问
index.php
中 ??application/config/rest.php
中的allowed_cors_headers
和allowed_cors_methods
是可以参考的原文:https://www.cnblogs.com/fsong/p/11258790.html