ng2-file-upload access-control origin issue

匆匆过客 提交于 2020-05-26 11:43:47

问题


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

  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



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!