Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng2-file-upload access-control origin issue

I have used this library for angular2 file upload https://github.com/valor-software/ng2-file-upload

Now I'm getting this error when I upload a file

XMLHttpRequest cannot load http://localhost:8080/files. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

like image 869
kohli Avatar asked Feb 17 '17 11:02

kohli


2 Answers

Make withCredentials = false before uploading the item. You can put this code in ngOnInit/ constructor or ngOnChanges.

this.uploader.onBeforeUploadItem = (item) => {
  item.withCredentials = false;
}
like image 65
Lahar Shah Avatar answered Oct 05 '22 21:10

Lahar Shah


Your server is responding with the following CORS Header

'Access-Control-Allow-Credentials' = true

This is a security that CORS provide, you are not allowed to do that. You cannot use Access-Control-Allow-Origin = * if you what to allow credentials. You will have to specify the exact domain. try Specifying

localhost:<portnumber>

For more information look at the following links

  1. Access-Control-Allow-Origin wildcard subdomains, ports and protocols
  2. Cross Origin Resource Sharing with Credentials
  3. CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
like image 23
Ankesh Avatar answered Oct 05 '22 20:10

Ankesh