我正在使用 Azure API 管理。 我将 CORS 策略添加到了我的入站部分。
我注意到,当原点不在允许的原点时,我会收到 200 状态代码和空响应正文,例如如果我的原始标头是 https://google.com。这是由于终止不匹配请求的行为造成的。
但是,我不希望响应正文为空。我希望我的响应正文返回 {"msg":"COR issues"}。我该怎么办?我知道那里有类似的问题,但到目前为止我找不到任何有效的解决方案。
<cors allow-credentials="false" terminate-unmatched-request="true">
<allowed-origins>
<origin>https://happygamer.com</origin>
</allowed-origins>
<allowed-methods>
<method>*</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
<expose-headers>
<header>*</header>
</expose-headers>
</cors>
使用给定的策略获取响应正文和状态代码以及 CORS 错误。
<policies>
<inbound>
<cors allow-credentials="false" terminate-unmatched-request="true">
<allowed-origins>
<origin>https://happygamer.com</origin>
</allowed-origins>
<allowed-methods>
<method>*</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
<expose-headers>
<header>*</header>
</expose-headers>
</cors>
<choose>
<when condition="@(context.Request.Headers.GetValueOrDefault("Origin") != null && context.Request.Headers.GetValueOrDefault("Origin") != "https://happygamer.com")">
<return-response>
<set-status code="403" reason="Forbidden" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{"msg":"CORS issue"}</set-body>
</return-response>
</when>
</choose>
</inbound>
</policies>
输出-