【发布时间】:2020-04-07 14:10:03
【问题描述】:
我正在使用 Azure APIM,我的 API 托管在由 .net core 编码的 Azure 应用服务上。我已经在 APIM 后面配置了我的 api。但是,当我尝试调用我的 API 时,我遇到了这个问题:
从原点获取 '' 的访问权限 'http://localhost:8080' 已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式为“no-cors”以获取禁用 CORS 的资源。
这是我调用我的 API 的 js 代码:
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 策略: https://docs.microsoft.com/en-us/azure/api-management/api-management-cross-domain-policies#CORS
但是,它并没有解决这个问题。那么我错过了什么吗?
提前致谢。
【问题讨论】: