• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enable cors from my local machine

#1
Hi,
i have a server with a normal domain and codeigniter 3.

To test a new project based on angular i would to do a query on DB server between Codeigniter.
Particularly i have a controller with all function that i need in my ajax call from my local machine. 
When i do ajax call browser return in console this error:

Access to XMLHttpRequest at 'https://xxxxx.it/xxxx' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried to add in my controller under parent:__constructor this headers:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
  die();
}


But unfortunatly not work.

Then i have insert in my htaccess this:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>


But not work.

Some one can help me to enable CORS on my codeigniter3 ?

Thanks for help
Reply

#2
You can add it to your .htaccess file in the root with index.php that's how I do it.


Code:
    # Add Font Types
    AddType application/vnd.ms-fontobject .eot
    AddType application/x-font-ttf        .ttf
    AddType application/x-font-opentype   .otf
    AddType application/font-woff         .woff
    AddType application/font-woff2        .woff2

    # Add Image Types
    AddType image/svg+xml .svg .svgz .jpg .png .ico

    <IfModule mod_headers.c>
        <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg|svgz|jpg|png|ico|font.css|css|js)$">
            ## un-remark this one for all access and remark out the one below it           
            #Header set Access-Control-Allow-Origin "*"
            ## Change this to your local host url. and https or http
            Header add Access-Control-Allow-Origin: "https://blog.local"
            Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
            Header add Access-Control-Allow-Headers: "Upgrade-Insecure-Requests"
        </FilesMatch>
    </IfModule>

    # Remove index.php from URL
    RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    RewriteCond %{THE_REQUEST}              ^[^/]*/index\.php [NC]
    RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]

Give that a try, take out what you do not need.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
I have change my htaccess with your, infact img not works, have change url allowed with localhost:4200 (from my local machine).

Nothing, same error
Reply

#4
Show me your .htaccess file, so that I can see what it is doing.

You can also read this.

How to enable Cross-Site XMLHttpRequests (Cors) using .htaccess
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.