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

459
Vistas
CORS on react axios, but not on vanilla

I am trying to get data from a AWS endpoint using React/axios, but when I try to make a request I get this CORS error:

Access to XMLHttpRequest at 'https://myAWS.com/login' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

when I try on my hosted github pages build, I also get the same error. But when I use Vanilla js on another hosted website it works, am I sendig my requests wrong on axios?

VANILLA

let body = {
    "name": "Bbbb 202",
    "email": "bbbb2220@bob.com",
    "password": "122",
    "wallet": "bbbbB202222"
  };
  
  var http = new XMLHttpRequest();
  var url = 'https://myAWS.com/login';
  http.open('POST', url, true);

  //Send the proper header information along with the request
  http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

  http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
      alert(http.responseText);
    }
  }
  http.send(body);

REACT

async SendRequest(method, url, callback, body){       
    axios.request({
        method: 'POST',
        data: JSON.stringify(body),
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "Authorization": "Bearer " + localStorage.getItem("token"),
        },
        url: url
    }) 
    .then(response => {
        console.log(response);
        callback({
            status: response.status,
            data: response.data,
        });
    })
    .catch(err => {
        console.log(err);
        callback({
            status: err.status,
            data: err,
        });
    });
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

These are not the same request. The 'vanilla' request looks like it might qualify for what's known as a 'safe request'. To qualify as a safe request, content-type must be application/x-www-form-urlencoded (or 2 others), and not use any restricted HTTP methods.

I wrote more extensively about it here as well: https://evertpot.com/no-cors/

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