0

When calling a ajax jquery request across two domanis I get this error

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

This is how the nginx conf looks like

location ~ \.php$ {
    include  /var/ini/nginx/fastcgi.conf;
    fastcgi_pass  php;
    fastcgi_param  SCRIPT_FILENAME $document_root/index.php;
    
    add_header  Access-Control-Allow-Origin  '*' always;
}

If I call the URL directly in the browser I get these response headers

access-control-allow-origin: *
cache-control: no-store, no-cache, must-revalidate
content-encoding: gzip
content-type: application/json
date: Tue, 14 Jun 2022 13:30:13 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT
pragma: no-cache
server: nginx
set-cookie: PHPSESSID=24uu79lr3quljgtjr19a1adqu4; path=/; HttpOnly
vary: Accept-Encoding

If I call the same resource via ajax jquery these are the response headers with HTTP 301

content-length: 162
content-type: text/html
date: Tue, 14 Jun 2022 13:30:22 GMT
location: https://xxx
server: nginx
strict-transport-security: max-age=31536000
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • Check how your AJAX request URI is looking. Most likely this redirect is configured in the nginx, and jQuery won't follow that redirect by default; check [this](https://stackoverflow.com/questions/12386272/301-redirect-on-ajax-redirected) SO thread. Depending on your nginx configuration, you may want to use [protocol relative URLs](https://en.wikipedia.org/wiki/Wikipedia:Protocol-relative_URL). – Ivan Shatsky Jun 14 '22 at 17:34
  • @IvanShatsky Great. There was a redirect with a trailing slash! :) create an answer :) – clarkk Jun 14 '22 at 18:22

0 Answers0