简体   繁体   中英

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. Now I want to call it from my PC at home, which logically doesnt have an own IP or domain. How am I supposed to set the Access-Control-Allow-Origin for my PC?

This is my function (I censored the API key):

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:

[EnableCors(origins: "*", headers: "*", methods: "*")]

And I get the following error:

Reason: CORS header 'Access-Control-Allow-Origin' missing

I also had to add the custom-headers in the Web.config file:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM