Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
blocked by CORS, No 'Access-Control-Allow-Origin' header - not working in react/axios but works fine in python/requests

The following python request is grabbing data from a paid API using private API key, and the fetch is working just fine:

import requests

api_key = 'our_private_api_key'
headers = { 'x-api-key': api_key }
params = {}
endpoint = 'our_api_endpoint_url'
resp: dict = requests.get(endpoint, headers=_headers_builder(headers), params=params).json().get('response', {})
resp_data = resp.get('data', None)

When fetching locally in Javascript (React App) using axios, we attempt with similar code, but in JavaScript (with the help of react-query's useQuery):

import { useQuery } from 'react-query';
import axios from 'axios'

async function fetchLiveData() {
    const api_key = 'our_private_api_key';
    const endpoint = 'our_api_endpoint_url';
    const { data } = await axios.get(endpoint, { headers: { 'x-api-key': api_key } });
    return data;
}

const { data, error, isError, isLoading: isLoading25 } = useQuery('posts', fetchLiveData);

...however the following error is thrown from this fetch.

Access to XMLHttpRequest at 'endpoint' 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.

Is this a localhost problem, or is some parameter being omitted from the axios fetch, or is it a permissions issue with the API provider? Or something else? How can we go about troubleshooting this issue?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I guess that the python env is outside the browser and the browsers block origin requestes and your client-side code don't have anything to do, the API is responsible of handling this issue by adding an http header "Access-Control-Allow-Origin: YOUR_DOMAIN OR *" and you said it's a paid API and you can't handle this so I assume that they have a console dashboard and I think in their settings you can configure ( whitelist ) the accepted origins and there you can add your origin, localhost or allow all origins.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try adding 'Access-Control-Allow-Origin': '*', in axios headers. Locally it should work. Good luck!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda