Welcome Guest, Not a member yet? Register   Sign In
CORS Policy
#1

(This post was last modified: 01-05-2023, 12:17 PM by AlexL.)

Hello Forum!
In CodeIgniter 4.2, when I make an AJAX call I get the following error:
net::ERR_FAILED 303
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This error ONLY appears when I use the website from my mobile. Neither on local nor on desktop I have any problem.
I have been searching for hours for information and have not been able to make any changes after trying everything I have found on the internet.
I tried this in htaccess
PHP Code:
<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://yoursite.com"
            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(.*)$            $[R=301,NS,L


But with this code with the htacces, instead of sending the ajax, the page refreshes. The ajax call does not fail, but it does nothing.
I also created a CORS filter but it didn't work either, what could be happening to make it only appear on my mobile and how can I fix it?
Thank you very much for your time

EDIT: Ajax and PHP is in the same URL
Reply
#2

Read https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Reply
#3

This is how it worked for me.
Code:
## .htaccess Control For CORS Configuration

# Add Font Awesome 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

<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
            ## very un-secure!!!
            #Header set Access-Control-Allow-Origin "*"
            ## Change this to your local host url. and https or http
            Header add Access-Control-Allow-Origin: "https://yoursite.com"
            Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
            Header add Access-Control-Allow-Headers: "Upgrade-Insecure-Requests"
        </FilesMatch>
</IfModule>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

https://onlinewebtutorblog.com/codeignit...tful-apis/

THis work for me
Reply
#5

See CodeIgniter 4 CORS configuration
Reply




Theme © iAndrew 2016 - Forum software by © MyBB