Flutter Web 和 Appwrite 在发布到我的 Web 服务器后出现 CORS 问题。

huangapple go评论78阅读模式
英文:

Flutter Web and Appwrite CORS problem after published to my web-server

问题

我在构建和发布我的Flutter Web应用程序之后遇到了问题。
当我从IDE(Android Studio)中运行它时,我的应用程序在本地运行时完美无缺,使用Google Chrome打开localhost:somePort。

我已经完成的步骤:

  • 将我的应用程序发布到我的Web服务器,并且在网站上正常运行,即www.example.com。
  • 在Appwrite控制台设置中为我的Appwrite应用程序创建了一个自定义域名,即ar.example.com。
  • 在域名的DNS设置中创建了A记录,通过浏览器可以访问Appwrite控制台的地址为https://ar.example.com/。
  • 根据文档在Appwrite服务器上创建了SSL证书。
  • 将Flutter应用程序中的Appwrite端点更改为新地址:https://ar.example.com/v1。
  • 修改了Appwrite服务器的appwrite文件夹中的.env文件以包括example.com和www.example.com。

尽管经过了所有这些步骤,但在浏览器中打开应用程序时,当尝试运行来自服务器的第一个命令时,应用程序会停止运行,浏览器的控制台会显示CORS错误,如下所示:

访问来自原始地址'https://example.com/'的XMLHttpRequest'https://ar.example.com/v1/account?#'已被CORS策略阻止:'Access-Control-Allow-Origin'标头的值为'https://localhost/',与提供的原始地址不相等。
英文:

I have a problem after I have built and published my Flutter Web App.
My app is working perfectly when running it from the IDE , Android Studio, with Google chrom in Localhost:somePort.

What I have done:

  • Published my app to my web server and it is working on the web-site with my domain, i.e. www.example.com

  • Created a custom domain in Appwrite console settings for my Appwrite app as ar.example.com,

  • Created A record in the DNS settings of domain, the console of Appwrite is accessible through the browsert at https://ar.example.com/,

  • Created an SSL certification on the Appwrite server according to the docs.

  • Changed the Endpoint of Appwrite in my Flutter app to point to the new address: https://ar.example.com/v1

  • I have modified the .env file in appwrite folder of the Appwrite server to the example.com and www.example.com.

and still after all that , I can open the app in the browser, but is stops running when it tries to run the first command from the server, browser's console gives me a CORS error as follows:

Access to XMLHttpRequest at 'https://ar.example.com/v1/account?#' from origin 'https://example.com/' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://localhost/' that is not equal to the supplied origin.

答案1

得分: 1

这个错误可能发生在以下情况下:

  1. 未调用 client.setProject() 方法
  2. 传递给 client.setProject() 方法的值不正确
  3. 项目中没有添加与应用程序主机名匹配的 Web 平台

此外,我不确定您是否应该执行以下步骤:

我已经修改了 Appwrite 服务器的 appwrite 文件夹中的 .env 文件,将其设置为 example.com 和 www.example.com

Appwrite 的 APP_DOMAIN* 环境变量应设置为您的主要 Appwrite 主机名。

英文:

This error can occur if:

  1. client.setProject() is not called
  2. the value passed to client.setProject() is incorrect
  3. there is no web platform added in your project that matches the hostname of your app

Also, I'm not sure you should be doing this step:

> I have modified the .env file in appwrite folder of the Appwrite server to the example.com and www.example.com.

The APP_DOMAIN* env vars for Appwrite should be set to your main Appwrite hostname.

答案2

得分: 0

我已经在Appwrite支持的帮助下找到了问题,希望这能帮助某些人。

在创建Appwrite项目时,您需要在项目的控制台设置中创建一个平台。

问题似乎是由于将平台创建为Flutter Web引起的,因为Flutter中定义的主机名期望为反向主机名,例如com.domainName.hostName,因此浏览器会将其识别为与客户端不同的域。

解决方案是创建一个Web平台,而不是Flutter Web平台。

Appwrite支持团队已承诺他们会仔细查看并修复必要的问题。

英文:

I have found the problem with the help of Appwrite support, hope this helps someone out there.

When creating a project in Appwrite you need to create a platform, in the project's console settings.

It appears that creating the platform as Flutter Web is causing this problem, since the hostname as defined in flutter is expecting a reverse host name, like com.domainName.hostName, and therefore recognized by the browser as a different domain than the client.

The solution is to create a Web platform instead of Flutter web platform.

Appwrite support team has promised they will carefuly look into it and fix what needed.

huangapple
  • 本文由 发表于 2023年7月27日 22:15:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定