【问题标题】:ng2-file-upload access-control origin issueng2-file-upload 访问控制起源问题
【发布时间】:2017-07-06 21:16:28
【问题描述】:

我已经使用这个库来上传 angular2 文件 https://github.com/valor-software/ng2-file-upload

现在我在上传文件时收到此错误

XMLHttpRequest 无法加载 http://localhost:8080/files。对预检请求的响应未通过访问控制检查:当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不得为通配符“*”。因此,不允许访问 Origin 'http://localhost:3000'。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。

【问题讨论】:

标签: angular file-upload


【解决方案1】:

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

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

【讨论】:

  • 如果我可以投票 100 次,我会这样做,这样就省去了我过去两天对谷歌存储上传的挫败感。
  • 我一直在寻找这个解决方案。谢谢
【解决方案2】:

您的服务器正在响应以下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

【讨论】:

  • 我添加了“Access-Control-Allow-Origin”,“*”这个
  • 你不能使用它,在你的标题中Access-Control-Allow-Credentialstrue,这不允许你使用,Access-Control-Allow-Origin 作为*。指定确切的来源localhost:3000 或将Access-Control-Allow-Credentials 设置为false
【解决方案3】:

你可以试试这个:

ngAfterViewInit() {
   this.uploader.onAfterAddingFile = (item => {
      item.withCredentials = false;
   });
}

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 2017-08-21
    • 2019-06-30
    • 2017-07-19
    • 2020-10-16
    • 2013-11-15
    • 2023-03-10
    相关资源
    最近更新 更多