Unfortunatly, CORS has to be handled on server side only. You need to create backend APIs that adds CORS onto the third-party APIs.
Here's an example on NodeJS
sample.js
const express = require('express');
const router = express.Router();
const axios = require('axios');
const cors = require('cors');
router.use(cors());
router.post('/', async function(req, res) {
try {
let data = req.body.name
const apiUrl = `https://sample.com/`;
const headers = {
Accept: "application/json",
"Content-Type": "application/json",
};
const apiResponse = await axios.get(apiUrl, { headers });
res.status(200);
res.send(apiResponse.data);
}
catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Something went wrong.' });
}
});
module.exports = router;
app.js
const express = require('express');
const bodyParser = require('body-parser');
var app = express();
const PORT = 8080;
var sample = require('./sample');
app.use(bodyParser.json());
app.use('/sample', sample);
app.listen(PORT, (error) =>{
if(!error)
console.log("Server is Successfully Running, and App is listening on port "+ PORT)
else
console.log("Error occurred, server can't start", error);
}
);
Packages used: express, body-parser, axios & cors