I keep getting this error message when I am sending a fetch request from the admin side. I want to connect to that API and get some response.
Access to fetch at
‘https://34557322456.execute-api.ap-southeast-1.amazonaws.com/V1/code?kdata=something’ from origin ‘https://mywebsite.online’ 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.
Is there a WordPress way to get it fixed?
Please help thanks
Tried these :
add_filter( 'allowed_http_origin', '__return_true' );
function add_cors_http_header(){
header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');
add_filter('allowed_http_origins', 'add_allowed_origins');
function add_allowed_origins($origins) {
$origins() = 'https://34557322456.execute-api.ap-southeast-1.amazonaws.com';
$origins() = 'https://34557322456.execute-api.ap-southeast-1.amazonaws.com/V1/code';
return $origins;
}
this is the fetch request
fetch('https://34557322456.execute-api.ap-southeast-1.amazonaws.com/V1/code?kdata=something', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}).then(response => {
return response.json()
})
.then(data => console.log(data))
.catch(error => console.log(error));