I'm using the auspost.com API to get a location from a Post Code - it works perfectly with PHP (on Load) - but not JS (on Form Change) - I'm new to APIs any help would be appreciated!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://digitalapi.auspost.com.au/postcode/search.json?q=3008',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'AUTH-KEY: MY_KEY_HERE'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The above works perfectly!
...this does not, I get the following error: MY_DOMAIN is not allowed by Access-Control-Allow-Origin. Status code: 200
var myHeaders = new Headers();
myHeaders.append("AUTH-KEY", "MY_AUTH_KEY");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://digitalapi.auspost.com.au/postcode/search.json?q=3008", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Since I have seen other questions referencing CORS policy - but I don't have control over the hosting or domain of the API - Is there anything I can do ay MY end to fix this?