CORS error on API call from SPFX web part

Hey,

I'm trying to create a client side SharePoint web part. Just to show the users work item request. I'm having issues with CORS from the Cireson API. I keep getting a No 'Access-Control-Allow-Orgin' header error. The request works in Postman. its just when I try to run it from the VSCode debugger I get this error. Any help or advice would be appreciated.

Error:

Access to fetch at '/api/V3/Authorization/GetToken?' from origin 'https://localhost:4321' 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.

Code:

var myHeaders = new Headers();

myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({

  "username": "XXXXX",

  "Password": "XXXXXXXX",

  "LanguageCode": "ENU"

});

var requestOptions = {

  method: 'POST',

  headers: myHeaders,

  body: raw,

  redirect: 'follow'

};

fetch("https://<domain>/api/V3/Authorization/GetToken?", requestOptions)

  .then(response => response.text())

  .then(result => console.log(result))

  .catch(error => console.log('error', error));

3 replies