简体   繁体   English

Codeigniter CORS 策略:没有“Access-Control-Allow-Origin”错误如何解决?

[英]Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve?

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.错误:从源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”标头。 Origin ' http://example.com ' is therefore not allowed access.因此,不允许访问来源“ 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?

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 *;
}

Allowing cross-site scripting may cause security issues, try to adjust your codeigniter options;允许跨站脚本可能会导致安全问题,尝试调整你的codeigniter选项;

  1. Go to application/config/config.php file,转到application/config/config.php文件,
  2. find $config['base_url'] = "";找到$config['base_url'] = ""; and
  3. place your project folder's path as value.将项目文件夹的路径作为值。

     $config['base_url']="http://localhost/yourProjectFolder/";

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";

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,你不需要启用它,因为它有安全隐患,只需执行以下操作

  1. open config.php ,打开config.php
  2. look for $config['base_url'] = "";寻找$config['base_url'] = "";
  3. change it to $config['base_url']="http://localhost/youproject/";将其更改为$config['base_url']="http://localhost/youproject/";

Save and reload your application.保存并重新加载您的应用程序。 Your good to go你很高兴

Add " Allow from all " in .htaccess file if it still doesn't work.如果仍然无法正常工作,请在.htaccess文件中添加“ Allow from all ”。

<FilesMatch ".(ttf|otf|eot|woff|woff2)$">
  <IfModule mod_headers.c>
    Allow from all
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

Add this directly to your php controller file:将此直接添加到您的 php 控制器文件中:

Header('Access-Control-Allow-Origin: *'); //for allow any domain, insecure
Header('Access-Control-Allow-Headers: *'); //for allow any headers, insecure
Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed

In my experience none of this answers was usable for me and the important item missed .以我的经验,这些答案对我来说都没有用,并且错过了重要的项目。 Using "*" is insecure.使用“*”是不安全的。

header("Access-Control-Allow-Headers: Origin,X-Requested-With");

Every where in web , experts just hint to little and common list of this headers.在 web 的每一个地方,专家都只是暗示这个标题的小而常见的列表。 If you are customized the headers for some reasons like authorization you need to use extended list like this.如果您出于某些原因(例如授权)自定义标题,则需要使用这样的扩展列表。 Use the headers related to your used options使用与您使用的选项相关的标题

header("Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept,Access-Control-Request-Method,Authorization,Cache-Control")

Use header() functions in your Codeigniter __construct在 Codeigniter __construct 中使用 header() 函数

    public function __construct()
            {
                parent::__construct();
                $this->load->model('api_model');
                $this->load->library('form_validation');
        
                Header('Access-Control-Allow-Origin: *'); //for allow any domain, insecure
                Header('Access-Control-Allow-Headers: *'); //for allow any headers, insecure
                Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed

//Or

        header('Access-Control-Allow-Origin: website_url');
        header("Content-Type: application/json; charset=UTF-8");
        Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed
            }

For my case, it thrown the same issue in the console.就我而言,它在控制台中引发了同样的问题。 I added the header in the file level as well as in the server level.我在文件级别和服务器级别添加了header It was still throwing the same error.它仍然抛出同样的错误。 Then I reached out to the respective vendors.然后我联系了各自的供应商。 They added the domain in their allowlist.他们将该域添加到了他们的许可名单中。 Afterwards, it started working.之后,它开始工作。 It did not throw any error in the console.它没有在控制台中引发任何错误。

So this could be one of the issue.所以这可能是问题之一。 And this may help others.这可能对其他人有所帮助。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM