1

I have issue with CORS policy. I am creating angular app and doing something wrong.

I am running Angular app on localhost:4200 and my api is running at XAMPP localhost. I want to load PDF from backend through url. I am using ' apiUrl + "navod/TS02-307_UM.pdf" ', where apiUrl is environment variable: apiUrl: 'http://localhost/'.

While I want to load PDF, I have issue with CORS policy. If I change url to: https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf. Everything working perfectly.

Where should be my mistake? :)

Rosta6
  • 161
  • 1
  • 4
  • 10

1 Answers1

1

Create a file called ".htaccess" in the directory of your files and add the following to the file.

Header set Access-Control-Allow-Origin "http://localhost:4200/"

or

Header set Access-Control-Allow-Origin "*"

CORS on apache

Check this flag in your httpd.conf :

AllowOverride All   <--- make sure this is not set to "None"

See also: Do you have to restart apache to make re-write rules in the .htaccess take effect?

Marc
  • 1,836
  • 1
  • 10
  • 14
  • I added ".htaccess" file in the root directory of localhost and it still not working. – Rosta6 May 25 '20 at 10:44
  • Can you copy it to navod/ ? After this do the check: apachectl -t (see link) https://enable-cors.org/server_apache.html – Marc May 25 '20 at 10:52
  • After move, it still not working. Check: Syntax OK I am using ng2-pdf-viewer for pdf display. – Rosta6 May 25 '20 at 10:57
  • Do have have an error log for " I have issue with CORS policy."? – Marc May 25 '20 at 11:07
  • Access to fetch at 'http://localhost/navod/TS02-307_UM.pdf' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. – Rosta6 May 25 '20 at 11:17
  • is there a http:// or a port missing? Can you see the outgoing url via web developer toolbar? What is the exact outgoing url? – Marc May 25 '20 at 11:31
  • Thank you. That was a simple and quick fix. I just added the .htaccess file you suggested and instantly our app could access the pdf files in that folder. – ideaztech Dec 23 '21 at 20:13