• Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Precios
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

107
Vistas
Why axios request doesnt work(Maybe problem in headers or CORS)?

The following Axios request made via a local server on Redux does not work, giving a CORS error:

    axios.post('server_url', qs.stringify({ 

      "username": "123", 
      "password": "123", 
      "repassword": "123"

    }, { 

      headers: {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded",
      },

    }))

But the request is made through Postman, and even the following python code:

import requests
from requests.structures import CaseInsensitiveDict

url = "server url"

headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/x-www-form-urlencoded"

data = "username='123'&password='123'&repassword='123'"

resp = requests.post(url, headers=headers, data=data)

print(resp.content)

There is no access to the server from the word at all. Maybe it's some kind of headings or something like that?

Error in console

Error in network

9 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A message or any request through axios must return a promise because the request to the server is an asynchronous operation. For example:

const url = "http://somedomen.org";
axios.post(
  url,
  { name: "John", pass: 1, repass: 1 },
  {
    headers: {
      Accept: "application/json",
      "Content-Type": "application/x-www-form-urlencoded",
    },
  },
).then(res => {
  return  res.data
})

Also you can use async/await sintaxis

async function func() {
  const url = "http://somedomen.org";

  const response = await axios.post(
    url,
    { name: "John", pass: 1, repass: 1 },
    {
      headers: {
        Accept: "application/json",
        "Content-Type": "application/x-www-form-urlencoded",
      },
    },
  )

  return response.data
}

Or to destructure the response variable

async function func() {
  
  const { data, status, headers } = await axios.post(
    //your condition...
  );

  return {
    data,
    status,
    headers,
  };
}

Until you have returned the promise of a result, the request cannot be completed. I hope I helped you

9 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.