gpt4 book ai didi

azure - 有没有办法在 Azure 虚拟机上启用 CORS?

转载 作者:行者123 更新时间:2023-12-02 08:21:13 25 4
gpt4 key购买 nike

有没有办法在 Azure 虚拟机上启用 CORS?我在 localhost:5001 运行 flask ,在 localhost:4200 运行 Angular 应用程序在我本地的环境中,它运行得很好。但是,在 azure 上的虚拟机实例中,它不起作用,因为我无法设置 CORS。

最佳答案

要在 azure 中启用 CORS,我们需要按照以下步骤在 API 中配置策略。

<强>1。添加 Azure 门户中所需的 API。

enter image description here

  • 在已部署的应用程序中应用以下更改如果 Web.config 文件中尚未完成,则 iis。

   

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>

要了解如何转到 web.config 文件,您可以引用 this Link to Configure CORS in azure

<强>2。转到 API >> 单击所有操作 >> 添加策略

enter image description here

- 在那里你可以看到 cors。点击它。

enter image description here

- 然后点击保存

enter image description here

- 您需要导航至入站策略并检查您是否有添加了此元素。 enter image description here

  • 您可以直接在表单编辑器或代码编辑器中进行编辑,如下所示:

enter image description here

  • 此示例演示了如何支持飞行前请求,例如那些具有自定义 header 或方法的。支持自定义 header 和
    其他 HTTP 动词,使用 allowed-methods 和 allowed-headers
    部分如下例所示。- 如果您的服务支持任何域的 COR,则将“*”放置为如下所示

enter image description here

(或)

  • 如果您希望仅通过少数特定 URI 来调用您的服务,您可以可以将它们添加到原始标签中,或者您可以使用直接填写表格表单编辑器。您可以添加您的服务所需的方法。

enter image description here

您可以引用this Document了解更多信息。

<小时/>

您可以启用 Access-Control-Allow-Credentials header 。当您需要发送 cookie 或 token 作为调用 API 的一部分时,这是必要的 >> Reference

<小时/>

(或)

  • 在 Cloud Shell 中,使用 az 为客户端的 URL 启用 CORSwebapp cors add 命令。替换占位符。

In AZURE CLI:

webapp cors add --resource-group myResourceGroup --name <app-name> --allowed-origins 'http://localhost:5000'
  • 您可以在以下位置设置多个客户端 URL:property.cors.allowedOrigins("['URL1','URL2',...]").你也可以启用所有带有“['*']”的客户端 URL。您可以引用this了解更多相关信息。

其他引用:

  1. Troubleshoot CORS error
  2. app-service-api-cors-consume
  3. Code examples-CORS
  4. App-service-web-tutorial-rest api
  5. CORS proxy
  6. Azure CDN with CORS

关于azure - 有没有办法在 Azure 虚拟机上启用 CORS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68263298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com