php Codeigniter CORS 政策:没有“Access-Control-Allow-Origin”错误如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41610470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve?
提问by Rushabh Shah
Error: Access to Font at 'http://www.example.com//assets/global/plugins/font-awesome/fonts/fontawesome-webfont.woff2?v=4.4.0' from origin 'http://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
错误:从源“ http://example”访问“ http://www.example.com//assets/global/plugins/font-awesome/fonts/fontawesome-webfont.woff2?v=4.4.0”中的字体.com' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“ http://example.com”。
Solution:
解决方案:
<?php
header('Access-Control-Allow-Origin: *');
class Home extends CI_Controller {
public function index()
{
$this->load->view('master');
}
}
?>
I tried this Solution but it not working can you please help me How to resolve it? and How to remove index.php from URL?
我试过这个解决方案但它不起作用你能帮我如何解决吗?以及如何从 URL 中删除 index.php?
回答by Tamer Durgun
Allowing cross-site scripting may cause security issues, try to adjust your codeigniter options;
允许跨站脚本可能会导致安全问题,请尝试调整您的 codeigniter 选项;
- Go to
application/config/config.php
file, - find
$config['base_url'] = "";
and place your project folder's path as value.
$config['base_url']="http://localhost/yourProjectFolder/";
- 去
application/config/config.php
档案, - 找到
$config['base_url'] = "";
并 将项目文件夹的路径作为值。
$config['base_url']="http://localhost/yourProjectFolder/";
回答by Latheesan
Try allowing GET & OPTIONS
尝试允许 GET & OPTIONS
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, OPTIONS");
If the above doesn't work, try allowing access to font resources via .htaccess
(for apache) or in nginx server block - add these lines:
如果上述方法不起作用,请尝试允许通过.htaccess
(对于 apache)或在 nginx 服务器块中访问字体资源- 添加以下行:
# Apache config
<FilesMatch ".(eot|ttf|otf|woff)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
or
或者
# nginx config
if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
add_header Access-Control-Allow-Origin *;
}
回答by Jacob Janga
Codeigniter is a cool framework to manipulate PHP, for CORS, you don't need to enable it as it has security implications, just do the following
Codeigniter 是一个很酷的 PHP 操作框架,对于 CORS,你不需要启用它,因为它有安全隐患,只需执行以下操作
- open
config.php
, - look for
$config['base_url'] = "";
- change it to
$config['base_url']="http://localhost/youproject/";
- 打开
config.php
, - 寻找
$config['base_url'] = "";
- 将其更改为
$config['base_url']="http://localhost/youproject/";
Save and reload your application. Your good to go
保存并重新加载您的应用程序。你好去
回答by Tamilheartz
Just we want add 'www' infront of domain name.
只是我们想在域名前面添加“www”。
Go to application/config/config.php file,
转到 application/config/config.php 文件,
$config['base_url']="http://yourdoamin.com";
Change to
改成
$config['base_url']="http://www.yourdoamin.com";
回答by Tefy Rak
Add "Allow from all" in .htaccess
file if it still doesn't work.
如果仍然不起作用,请在文件中添加“ Allow from all” .htaccess
。
<FilesMatch ".(ttf|otf|eot|woff|woff2)$">
<IfModule mod_headers.c>
Allow from all
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>