Axios: I set method 'POST' but Request method is considered as 'OPTIONS'

Created on 10 Apr 2018  路  6Comments  路  Source: axios/axios

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'

2018-04-10_1355

What 's wrong?
Thanks

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.

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Spartano picture Spartano  路  3Comments

Shu-Ji picture Shu-Ji  路  3Comments

ghost picture ghost  路  3Comments

Baoyx007 picture Baoyx007  路  3Comments

c0debreaker picture c0debreaker  路  3Comments