[英]How to add CORS header to my REST API if I dont have a domain?
I am working on a REST API and it runs on an extern server with an own public IP.我正在研究 REST API,它在具有自己的公共 IP 的外部服务器上运行。 Now I want to call it from my PC at home, which logically doesnt have an own IP or domain.现在我想从家里的电脑上调用它,这在逻辑上没有自己的 IP 或域。 How am I supposed to set the Access-Control-Allow-Origin for my PC?我应该如何为我的 PC 设置 Access-Control-Allow-Origin?
This is my function (I censored the API key):这是我的 function (我审查了 API 密钥):
async function loadTelluzApi(url) {
const response = await fetch(url, {
method: 'GET',
// body: myBody, // string or object
headers: {
'ApiKey': 'apikey'
}
});
console.log(response.json())
return await response.json();
}
For test-purposes I added this above the controler of my REST API:出于测试目的,我在 REST API 的控制器上方添加了这个:
[EnableCors(origins: "*", headers: "*", methods: "*")]
And I get the following error:我收到以下错误:
Reason: CORS header 'Access-Control-Allow-Origin' missing原因:缺少 CORS header 'Access-Control-Allow-Origin'
I also had to add the custom-headers in the Web.config file:我还必须在 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,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.