http.get请求工作正常。当api被移动到服务器时。,出现了问题。
客户端使用angularJs
$http.get('http://example.com/api/spots/2/0').success(function(data){
console.log(data);
});
日志给出:阻止跨来源请求:同源策略不允许读取上的远程资源http://example.com/api/spots/2/0.这可以通过将资源移动到同一域或启用CORS来解决。
我已经将这两行添加到我的控制器构造中
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
仍然是相同的错误。
尝试将OPTIONS
添加到允许的方法中。
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
并在设置了标头后,当请求为方法"OPTIONS"时立即返回。
if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
die();
}
另请参阅此答案。
Angular发送一个符合W3C CORS规范的飞行前请求,在实际尝试之前会检查允许的正确方法
就我个人而言,我发现Mozilla开发者网络CORS页面更容易阅读,有助于理解CORS的流程。
如果其他人面临这个问题,在Codeigniter rest Controller的rest.php文件中启用CORS对我来说很有效https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/config/rest.php
//Change this to TRUE
$config['check_cors'] = TRUE;
//No change here
$config['allowed_cors_headers'] = [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'Access-Control-Request-Method',
'Authorization',
];
//No change here
$config['allowed_cors_methods'] = [
'GET',
'POST',
'OPTIONS',
'PUT',
'PATCH',
'DELETE'
];
//Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any source domain
$config['allow_any_cors_domain'] = TRUE;
//Used if $config['check_cors'] is set to TRUE and $config['allow_any_cors_domain'] is set to FALSE.
//Set all the allowable domains within the array
//e.g. $config['allowed_origins'] =['http://www.example.com','https://spa.example.com']
$config['allowed_cors_origins'] = [];
我在控制器类中添加了以下构造函数
public function __construct($config = 'rest')
{
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
parent::__construct();
}
客户端=>AngularJs(在localhost:9000中与Grunt一起运行)服务器端=>php(codeIgniter解决方案)(在localhost:80中运行)
唯一对我有用的是将这几行添加到我的php项目中的Web服务控制器中:
/*
here you do whatever you do to build the $data
*/
//but just before returning the method data add this
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
echo json_encode($data, JSON_NUMERIC_CHECK);
在控制器的父构造函数中添加以下代码
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, OPTIONS, POST, GET, PUT");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
将其添加到config.php以便发送access-control-allow-origin
HTTP标头以接受来自任何地方的连接。
$method = $_SERVER["REQUEST_METHOD"];
if ($method == 'OPTIONS') {
header("access-control-allow-origin: *");
die("");
}
如果您可以使用jQueryAjax,那么在脚本中使用此行。
jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5)
当我尝试使用jQueryAjax将一些字符串从侧边栏桌面小工具发布到xamp.php文件时,它解决了我的问题。
要通过@amal-ajith添加到答案中,应在rest.php文件中添加头。例如,我需要为Ionic 4应用程序api调用/请求添加授权令牌,我所需要做的就是在rest.php文件中添加头字段,我的cors错误就得到了处理。
Access to XMLHttpRequest at 'http://localhost/ci/index.php/api/validate_token' from origin 'http://localhost:8100' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
//No change here
$config['allowed_cors_headers'] = [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'Access-Control-Request-Method',
'Authorization'
];
这里有很多可能的解决方案,但没有一个对我有效。我挖了一些东西,发现了一些东西。我不想解释,但我希望对你有用。
Add the following block of code in your controller file .
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
对我来说,我在一个路由控制器中使用了curl。我正在使用代码点火器4。
所以制作一个控制器来命名你想要的任何东西。
制定方法
在方法中使用卷曲:
public function index() { //code using curl or file get contents depending upon your api}
使用这样的路线:在我们的案例索引中使用/controller/method
参考:https://codeigniter4.github.io/userguide/incoming/controllers.html