Hi,
I just got this issue in my project.
Back-end and Front-end are divided.
axios.post('http://192.168.4.234:3000/api/v1/campaigns', {title: 'here', content: 'ddd'})
but request method is considered as 'OPTIONS'
What 's wrong?
Thanks
For cross origin requests, the server needs to set the "Access-Control-Allow-Origin" header. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
A browser sends an OPTIONS request before a POST request, essentially to check with the server that it's allowed to send the POST request.
You need a method in your server that catches OPTIONS requests, and responds with the correct headers as szahn has alluded to above (you need to make sure Access-Control-Allow-Methods
is sent). If the right answer comes back (200), the browser follows up by sending the POST request.
Thanks. It was not responsible for axios. Issue was in backend.
BAM! Thank you @ralphking! I was looking for that little piece of info for more than an hour!
I solved this issue setting withCredentials: false.
Example:
call1 = axios.get(
"www.foo.bar",
{
withCredentials: false,
headers: {
'key': '12345',
},
params: {
id: 'abcde'
}
}
)
I solved this issue setting withCredentials: false.
Example:
call1 = axios.get( "www.foo.bar", { withCredentials: false, headers: { 'key': '12345', }, params: { id: 'abcde' } } )
no you didn't... you're sending a 'get' request, the issue is with 'post' requests.
Most helpful comment
A browser sends an OPTIONS request before a POST request, essentially to check with the server that it's allowed to send the POST request.
You need a method in your server that catches OPTIONS requests, and responds with the correct headers as szahn has alluded to above (you need to make sure
Access-Control-Allow-Methods
is sent). If the right answer comes back (200), the browser follows up by sending the POST request.