问题
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.
回答1:
Make withCredentials = false
before uploading the item. You can put this code in ngOnInit
/ constructor
or ngOnChanges
.
this.uploader.onBeforeUploadItem = (item) => {
item.withCredentials = false;
}
回答2:
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
- Access-Control-Allow-Origin wildcard subdomains, ports and protocols
- Cross Origin Resource Sharing with Credentials
- CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
回答3:
You can try this:
ngAfterViewInit() {
this.uploader.onAfterAddingFile = (item => {
item.withCredentials = false;
});
}
来源:https://stackoverflow.com/questions/42296349/ng2-file-upload-access-control-origin-issue