Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

105
Views
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

7 months ago · Santiago Trujillo
1 answers
Answer question

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

7 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.