2017-02-17 46 views
2

我已经使用这个库angular2文件上传https://github.com/valor-software/ng2-file-uploadNG2文件上传访问控制起源问题

现在我得到这个错误,当我上传文件

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. 
+0

这解决了我的同样问题https://stackoverflow.com/a/47334113/5236174,请检查它是否是您的案例中的可接受答案。 –

回答

1

您的服务器响应具有以下CORS Header

'Access-Control-Allow-Credentials' = true 

这是一个安全性CORS提供,你不能这样做。您不能使用Access-Control-Allow-Origin = *如果您允许凭据。你将不得不指定确切的域名。尝试指定

localhost:<portnumber> 

欲了解更多信息,看看下面的链接

  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
+0

我已添加“Access-Control-Allow-Origin”,“*”这一个 – kohli

+0

你不能使用它,在你的头文件中'Access-Control-Allow-Credentials'即将来临'true',它不允许你使用“Access-Control-Allow-Origin”作为'*'。 指定一个确切的起源'localhost:3000'或一些如何将'Access-Control-Allow-Credentials'设置为false – Ankesh

4

withCredentials = false上传项目之前。您可以将此代码放入ngOnInit/constructorngOnChanges

this.uploader.onBeforeUploadItem = (item) => { 
    item.withCredentials = false; 
} 
+0

这种方式就像一个魅力。 Thankyou这么多:) –

+0

如果我可以upvote 100倍,我会这样做,这节省了我过去两天谷歌存储上传的挫折感。 – Vignesh