Codeigniter CORS policy: No 'Access-Control-Al

2019-01-25 22:10发布

问题:

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.

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?

回答1:

Try allowing 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:

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


回答2:

Allowing cross-site scripting may cause security issues, try to adjust your codeigniter options;

  1. Go to application/config/config.php file,
  2. find $config['base_url'] = ""; and
  3. place your project folder's path as value.

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


回答3:

Add "Allow from all" in .htaccess file if it still doesn't work.

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


回答4:

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

  1. open config.php,
  2. look for $config['base_url'] = "";
  3. change it to $config['base_url']="http://localhost/youproject/";

Save and reload your application. Your good to go



回答5:

Just we want add 'www' infront of domain name.

Go to application/config/config.php file,

 $config['base_url']="http://yourdoamin.com";

Change to

 $config['base_url']="http://www.yourdoamin.com";