I'm creating a react app using the Laravel API and sending requests through Axios but a preflight OPTION request is sent with every GET or POST request
although these preflight requests are not causing me any trouble, I just want to get them out of the way.
I've done enough reading on CORS to know that only simple requests don't need preflight requests, and since my requests contain authorization headers, they are not considered simple.
I've tried:
- adding a proxy field to my package.json
- creating a middleware for preflight requests
There was no success with any of the above.
As far as I can tell, this is the only answer that will work, Why is an OPTIONS request sent and can I disable it?, But I have no idea where to add this header: Access-Control-Allow-Origin: *
Or I can add this: Access-Control-Max-Age: 600 , Yet again, I'm not sure where it belongs. I mean, I am not sending the option request, so how am I supposed to set headers for something I am not even sending?
Okay, enough with the explaining, here is my post request:
axios.post(`/ticket/create`, formData).then(res => {
if (res.status === 201) {
alert('success')
} else {
alert('request failed')
}
})
Here are my axios configs:
axios.defaults.baseURL = 'http://127.0.0.1:8000/api';
axios.interceptors.request.use(function (config) {
const token = localStorage.getItem('auth_token');
config.headers.Authorization = token ? `Bearer ${token}` : '';
return config;
})