简体   繁体   English

如何将 CORS header 添加到我的 REST ZDB974238714CA8DE634A7CE1D083A1 如果没有域?

[英]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.

相关问题 如何在Django REST API上调试启用CORS - How do I debug enabling CORS on my Django REST api 如何将 CORS-Header 添加到我的 esp32 网络服务器 - How to add CORS-Header to my esp32 webserver 如何使用带有JQuery的小书签发送403身份验证标头作为跨域请求(CORS)? - How can I send a 403 Authentication header, as a cross domain request (CORS) , using a bookmarklet with JQuery? 我想给我的javascript函数添加延迟,但是不知道如何 - I want to add a delay to my javascript function but dont know how 如何使用Javascript访问JIRA REST API而不使用&#39;CORS&#39;chrome扩展名来解决&#39;跨源域&#39;问题 - How to resolve 'Cross Origin domain' issue without using 'CORS' chrome extension using Javascript to access JIRA REST API Yammer REST API CORS - Yammer REST API CORS REST API - CORS 问题 - REST API - CORS Problems 如何使用Service Worker为我的所有API请求添加Authorization标头,并且有意义吗? - How can I, and does it make sense to, use a Service Worker to add an Authorization header to all my API requests? 在获取 api 时发布请求 CORS 错误(Access-Control-Allow-Origin),即使我在 header 上添加了访问控制 - Post Request CORS Error (Access-Control-Allow-Origin) on fetch api even i added access control on my header 如何在不使用CORS的情况下使用JavaScript连接到跨域API - How to connect to a cross domain API using javascript without CORS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM