I am creating a simple application with Vue, and I call an endpoint with axios
axios.post(url, {
email: this.email,
})
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
I get the error
from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
The problem is not at the server level, I have done tests from Postman or directly with CURL and it does not generate an error.
Solution:
Thanks to Shrinivas Bendkhale's comments. I managed to solve the problem.
At first it did not work, so it was necessary to add the "logLevel" and "pathRewrite" options to the proxy.
- logLevel: It allows to see in the terminal how the proxy is working
- pathRewrite: Add the rest of the path to the proxy
// vue.config.js
module.exports = {
devServer: {
proxy: {
'^/rp': {
target: process.env.API_OLD_SERVER,
secure: false,
logLevel: 'debug',
pathRewrite: {
'^/rp': '/'
}
},
},
},
}
So my call was as follows
axios.post('/rp/full-path', {
usermail: this.email,
})
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});