简体   繁体   English

Firebase 托管的 Flutter Web/Dart CORS 错误

[英]Flutter Web/Dart CORS Error with Firebase Hosting

Problem问题

I wrote an application in Flutter Web.我在 Flutter Web 中编写了一个应用程序。 When I run it in the Browser (debug), I get this error:当我在浏览器(调试)中运行它时,出现此错误:

cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.googleapis.com/identityto...

When I run it in release mode, I just get this:当我在发布模式下运行它时,我得到了这个:

Error while fetching an original source: NetworkError when attempting to fetch resource.
Source URL: org-dartlang-sdk:///sdk/lib/_internal/js_runtime/lib/js_helper.dart

Other info其他信息

  • The App is hosted in Firebase Hosting but the error also occurs on localhost without Firebase该应用程序托管在 Firebase Hosting 中,但错误也会发生在没有 Firebase 的本地主机上
  • I think the Problem is cors in both cases but the release mode just has less logs我认为这两种情况下的问题都是 cors,但发布模式的日志较少

What I tried我试过的

According to this Documentation or this Question I have to add something using Expressjs like:根据文档或问题,我必须使用 Expressjs 添加一些内容,例如:

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({ origin: true }));
  • Is there anything like Expressjs in Dart/Flutter? Dart/Flutter 中是否有类似 Expressjs 的东西? I saw this but I could not get it to work.我看到了这个,但我无法让它工作。
  • Or is there another way to set the headers?或者有另一种设置标题的方法吗?

It seems that indeed, CORS is blocking you from using Firebase to store and access data.似乎确实,CORS 阻止您使用 Firebase 来存储和访问数据。 As per the documentation Configuring cross-origin resource sharing (CORS) :根据文档配置跨域资源共享(CORS)

Cross Origin Resource Sharing (CORS) is a mechanism for allowing interactions between resources from different origins, something that is normally prohibited in order to prevent malicious behavior.跨源资源共享 (CORS) 是一种允许来自不同来源的资源之间进行交互的机制,这通常是为了防止恶意行为而被禁止的。

Considering that, you will need to configure yours to work and accept requests made via HTTP.考虑到这一点,您需要配置您的工作并接受通过 HTTP 发出的请求。 I would recommend you to take a look at the above documentation and the following links, so you can get more information about it.我建议您查看上述文档和以下链接,以便获得更多相关信息。

Once you have them configured, you should not face both errors anymore, since the requests via HTTP will be authorized via CORS and the access to Firebase should be established.配置它们后,您不应再面临这两个错误,因为通过 HTTP 的请求将通过 CORS 进行授权,并且应该建立对 Firebase 的访问。

Let me know if the information helped you!如果这些信息对您有帮助,请告诉我!

So I was at the same point you were and didn't really want to create Cloud Functions to solve this.所以我和你一样,并不真的想创建 Cloud Functions 来解决这个问题。 I found out that there is a possibility to edit the CORS configuration on firebase.我发现可以在 firebase 上编辑 CORS 配置。 It's ab it complicated:这很复杂:

You'll need to install gsutil, authorise it and then edit the CORS configuration.您需要安装 gsutil,对其进行授权,然后编辑 CORS 配置。

You can find more here on the official firebase page您可以在官方 firebase 页面上找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter web 在 firebase 托管中不工作 - Flutter web not working in firebase hosting Flutter Web 与 Firebase,错误“dart_sdk.js 没有模块” - Flutter Web with Firebase, error "No module for dart_sdk.js" Flutter websockets 在本地 web 但不在 Firebase 中工作 - Flutter websockets working in local web but not in Firebase Hosting 在Firebase托管中启用CORS - Enabling CORS in Firebase Hosting Firebase 托管 - Flutter Web extra.ZFC35FDC70D5FC69D2539883A82EZC页面的应用程序托管 - Firebase Hosting - Flutter Web App hosting of extra .html page Flutter Web Firebase Hosting XMLHttpRequest 错误(使用本地 chrome 浏览器(调试)或使用 Firefox 没有错误) - Flutter Web Firebase Hosting XMLHttpRequest error (no error using local chrome browser (debug), or using Firefox) 使用 Firebase 托管和 Cloud Run 时出现 CORS 错误 - CORS error when using Firebase Hosting and Cloud Run Flutter Web Firebase TypeError: dart.global.firebase.firestore is not a function - Flutter Web Firebase TypeError: dart.global.firebase.firestore is not a function 在npm中为网络托管安装Firebase时出错 - Error in installing Firebase in npm for Web hosting 使用 Codemagic CI/CD 自动将 Flutter Web 项目部署到 Firebase 托管 - Automatically Deploy Flutter Web Project to Firebase Hosting with Codemagic CI/CD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM