Hi!
I have a piece of javascript code embedded on a confluence page which intends to retrieve results from a third party API. Whenever I execute the javascript code with the call to the RESTfull API I get the following error message:
- Access to fetch at 'https://api.thirdparty.com/endpoint' from origin 'https://portal.mycompany.com' 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.
This Confluence page is hosted on a server owned by my company but I don't have admin access to the server. Does anyone know how I can solve this CORS issue? Below the code I'm using:
<!DOCTYPE html>
<html>
<button onclick="AccountBalance()">Get Balance</button>
<script>
'use strict'
function AccountBalance() {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Basic xpto");
var raw = JSON.stringify({
"testcaseId": "123456",
"database": "kpi",
"table": "Main"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
credentials: 'include',
redirect: 'follow'
};
fetch('https://api.thirdparty.com/endpoint', requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
</script>
</html>
Thanks!
BR