I have a problem with CORS while sending POST request:
Here is my code:
import axios from 'axios'
const api = axios.create({
baseURL: 'http://localhost:8084',
})
export const postTip = async (payload) => {
try {
const { data } = await api.post(`post-tip`, payload);
return data;
} catch (e) {
return [];
}
};
I have found that I need to set Access-Control-Allow-Origin
in headers, and I did it in 2 ways. First one:
const api = axios.create({
baseURL: 'http://localhost:8084',
headers: {
post: {
"Access-Control-Allow-Origin": true
}
}
})
And the second one:
const { data } = await api.post(`post-tip`, payload, {
headers: {
"Access-Control-Allow-Origin": true
}
});
Both of them work and in request header I can see this line Access-Control-Allow-Origin: true
. But I still get this error, so, what's the problem?
I have a problem with CORS while sending POST request:
Here is my code:
import axios from 'axios'
const api = axios.create({
baseURL: 'http://localhost:8084',
})
export const postTip = async (payload) => {
try {
const { data } = await api.post(`post-tip`, payload);
return data;
} catch (e) {
return [];
}
};
I have found that I need to set Access-Control-Allow-Origin
in headers, and I did it in 2 ways. First one:
const api = axios.create({
baseURL: 'http://localhost:8084',
headers: {
post: {
"Access-Control-Allow-Origin": true
}
}
})
And the second one:
const { data } = await api.post(`post-tip`, payload, {
headers: {
"Access-Control-Allow-Origin": true
}
});
Both of them work and in request header I can see this line Access-Control-Allow-Origin: true
. But I still get this error, so, what's the problem?
2 Answers
Reset to default 2This error from your backend, you use axios post from front-end, but you need to set header "Access-Control-Allow-Origin": true on your backend.
this error is probably ing from the server. Are you trying to reach your own api? If you do the error might be there.
I see you tagged express so please refer to this page it should contain all the information you need
http://expressjs./en/resources/middleware/cors.html