AJAX 和 Azure 反向代理的 CORS 问题



我在使用CORS时遇到问题。我通过域 B 将 AJAX 调用从域 A 中的 SPA 调用到域 C(我的)中的 REST API,我已将其设置为反向代理和负载均衡器。这三者都部署到 Azure Web 应用。

从 A 到 C 的呼叫直接工作,没有问题。但是通过 B 从 A 到 C 的调用将不起作用。

允许使用门户在 B 和 C 上从 A 进行 CORS。允许使用门户在 C 上从 B 开始 CORS。

API 服务器 C 的 Web.config 具有以下内容:

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

但是我收到以下错误:

    Response to preflight request doesn't pass access control check: 
    No 'Access-Control-Allow-Origin' header is present on the requested resource. 
    Origin 'null' is therefore not allowed access. 
    The response had HTTP status code 400.

所以,我的理解是API服务器必须允许SPA发出请求,这就是我认为上面的行所做的。那么有谁知道这是怎么回事?

可以在应用服务中控制 CORS,该设置可能会替代 web.config。在"应用服务"菜单中,向下滚动到"API"部分,有一个 CORS 菜单。

由于您指定了一些允许的标头和方法,因此该部分可能不适合您,但是如果您为 * 添加记录,然后将其删除,它应该开始使用基于 web.config 的 CORS 处理。

最新更新