Got this error from Binance:
has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
Calling like this in a React app.
useEffect(() => {
const fetchData = async () => {
const { data } = await axios.get('https://binance.com/api/v3/klines', {
headers: { crossDomain: true },
params: {
symbol: 'ETHUSDT',
interval: '1h',
startTime: '1514764800000',
},
})
fetchData()
}, [])
From Postman it works, what is the problem?
Full error:
bot:1 Access to XMLHttpRequest at 'https://binance.com/api/v3/klines?symbol=ETHUSDT&interval=1h&startTime=1514764800000' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
tried withCredentials: true
, did not work
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
const { data } = await axios.get('https://binance.com/api/v3/klines', {
withCredentials: true,
headers: {},
tried proxy:
const { data } = await axios.get('http://localhost:3000/api2/klines', {
and set rewrite next.config.js
:
async rewrites() {
return [
{
source: '/api2/:path*',
destination: `https://binance.com/api/v3/:path*`,
},
]
},
got this error:
has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.