I am trying to send a POST request to an API with multipart data.
I test the API in postman and everything works fine in Postman. But when I call the API in react, it gives me CORS error.
I cross-checked the URL, Header, and Data, all seems OK for me. I go through multiple Stack Overflow question on the same topic and found that I need to pass allow-cross-origin along with the header. I added that in my header but didn't able to solve my problem.
The error which I got in the console is:
No 'Access-Control-Allow-Origin' header is present on the requested resource
API Calling Code
import axios from 'axios';
const header = {
"userid":localStorage.getItem("userid"),
"token":localStorage.getItem("token"),
"Content-Type": "multipart/form-data",
"Access-Control-Allow-Origin": "*"
}
const URL="https://api.hello.com/dashboard/venue_updated";
export function updateVenue(data,name,venue_type,email, phone_no,callback, errorcallback){
console.log(header);
axios.post(URL,data,{
params:{
name,
venue_type,
email,
phone_no,
},
headers:header
})
.then(res => {
if(callback != null){
callback(res);
}
})
.catch(err => {
if(errorcallback != null){
errorcallback(err);
}
})
}
I used to import this in my component and call it on the form-submit method.