-2

I get the following error code with my script:

https://XXX.XXX.XX.XXX:8443/backend/rs/shipments/' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I postman everything works well, but my script ist denied every time. I tried to set all relevant options in IIS of my Windows Webserver which I want to address with my API-request.

SERVER-CONFIG:

Werbserver IIS HTTP-Webserver settings

document.getElementById('sendRequestButton').addEventListener('click', async () => {
  const url = 'https://xxx.xxx.xx.xxx:8443/backend/rs/shipments/';

  const myHeaders = new Headers();
  myHeaders.append("Content-Type", "application/glsVersion1+json");
  myHeaders.append("Accept", "application/glsVersion1+json, application/json");
  myHeaders.append("Authorization", "xxxx");

  const raw = JSON.stringify({
    Shipment: {,
      Shipper: {
        ContactID: "xxxxxx"
      },
      ShipmentUnit: [
        {
          Weight: 5
        }
      ]
    },
    PrintingOptions: {
      ReturnLabels: {
        TemplateSet: "NONE",
        LabelFormat: "PDF"
      }
    }
  });

  const requestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: raw,
    redirect: 'follow'
  };

  try {
    const response = await fetch(url, requestOptions);
    const result = await response.text(); // Verwenden Sie .text(), wenn die Antwort kein JSON ist
    document.getElementById('responseOutput').textContent = result;
  } catch (error) {
    console.error('Error:', error);
    document.getElementById('responseOutput').textContent = `Error: ${error}`;
  }
});

I tried to get the correct settings in my webserver with IIS but don't know what to to any more. Every time I run my script locally it fails with the shown error

6
  • 2
  • this is not a duplicate, the other question asks why and i need a solution how the request can work and wihich settings i need to do on my webserver with iis
    – LEon
    Commented Jul 5 at 11:54
  • 1
    Have you configured CORS on the server? If so, how? Edit your question and add details about that.
    – jub0bs
    Commented Jul 5 at 12:01
  • 1
    It says from origin 'null' in your error message, which usually means you are not making this request from a page loaded via HTTP, but from a document you only loaded via the file system.
    – CBroe
    Commented Jul 8 at 10:37
  • yeah, it is from a local file. which setting i have to do in the cors settings of my webserver?
    – LEon
    Commented Jul 8 at 11:29

0

Browse other questions tagged or ask your own question.