由于CORS问题,未能从Azure APIM后面的网页调用API
我使用的是Azure APIM,我的API托管在由.net核心编码的Azure应用程序服务上。我已经在APIM后面配置了API。但是,当我尝试调用我的API时,我遇到了以下问题: 从原点获取的访问权限 已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源如果不透明的响应满足您的需要,请设置请求的 模式设置为“无cors”,以获取禁用cors的资源 这是我称之为API的js代码:由于CORS问题,未能从Azure APIM后面的网页调用API,azure,cors,Azure,Cors,我使用的是Azure APIM,我的API托管在由.net核心编码的Azure应用程序服务上。我已经在APIM后面配置了API。但是,当我尝试调用我的API时,我遇到了以下问题: 从原点获取的访问权限 已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源如果不透明的响应满足您的需要,请设置请求的 模式设置为“无cors”,以获取禁用cors的资源 这是我称之为API的js代码: var url='&
var url='<the url of my api in APIM>';
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Seckey":"xxxxxx"
},
body: '<some json content>'
}).then(function(res) {
console.log("Response succeeded?", JSON.stringify(res.status));
console.log(JSON.stringify(res));
}).catch(function(e) {
console.log("fetch fail", JSON.stringify(e));
});
我知道这是一个CORS问题,我已经根据此文档在APIM中配置了CORS策略:
然而,它并没有解决这个问题。我错过什么了吗
提前感谢。正如@Thiago Custodio所说,您应该在Azure应用程序服务和APIM中配置CRO 顺便说一句,如果您已经为Azure应用程序服务启用了CRO,请检查您是否在APIM中正确配置了CORS。根据您的请求,我注意到您有一个自定义标题:Seckey,您是否在CORS策略中配置了它 如果没有,请尝试下面的CORS政策,否则您将遇到CORS问题:
<cors >
<allowed-origins>
<origin>http://localhost:8080/</origin>
</allowed-origins>
<allowed-methods preflight-result-max-age="300">
<method>POST</method>
</allowed-methods>
<allowed-headers>
<header>Content-Type</header>
<header>Seckey</header>
</allowed-headers>
</cors>
希望能有帮助 您的保单放在哪里了?CORS只能在API和操作级别工作,而不能在产品级别工作。 有关APIM中策略的范围,请参见本博客。 这是从Azure APIM CORS策略的一个示例中摘录的,该策略是有效的
<inbound>
</base>
<cors allow-credentials="true">
<allowed-origins>
<origin>http://localhost/</origin>
</allowed-origins>
<allowed-methods preflight-result-max-age="300">
<method>GET</method>
<method>POST</method>
</allowed-methods>
<allowed-headers>
<header>Authorization</header>
<header>Ocp-Apim-Subscription-Key</header>
<header>content-type</header>
</allowed-headers>
</cors>
</inbound>
<outbound>
<base />
<!--CORS-->
<set-header name="Access-Control-Allow-Credentials" exists-action="override">
<value>true</value>
</set-header>
<set-header name="Access-Control-Allow-Headers" exists-action="override">
<value>*</value>
</set-header>
</outbound>
一些文档建议也在出站中添加此标题
<set-header name="Access-Control-Allow-Origin" exists-action="override">
<value>@(context.Request.Headers.GetValueOrDefault("Origin",""))</value>
</set-header>