I am trying to integrate DHL API for rate estimate in my website. I am using axios in the frontend to achieve this but i keep getting the CORS POLICY ERROR "Access to XMLHttpRequest at 'https://express.api.dhl.com/mydhlapi/test? from origin 'http://localhost:3000' 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."
I am not using a backend server, hence i can’t get around this error. What should i do in my code to get around this error and get the result back from the API?
here is my code:
const getRateEstimate = () => {
const options = {
method: 'GET',
url: 'https://express.api.dhl.com/mydhlapi/test',
params: {
accountNumber: 'myaccnumber',
originCountryCode: fromCountriesCode,
originCityName: fromCountriesCapital,
destinationCountryCode: toCountriesCode,
destinationCityName: toCountriesCapital,
weight: weight,
length: '5',
width: '5',
height: '5',
plannedShippingDate: date,
isCustomsDeclarable: 'false',
unitOfMeasurement: 'metric',
},
headers: {
Authorization: 'Basic authkey',
}
};
axios.request(options).then((response) => {
console.log(response.data);
setData(response.data);
}).catch((error) => {
console.error(error);
});
}