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?
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