I have a chrome extension which uses react/axios. In that app I'm sending a post request like so:
export const createComment = payload => { const url = `${COMMENTS_BASE_URL}`; const promise = axios.post(url, payload); return { type: CREATE_COMMENT, promise }; }
Even though it's clearly axios.post(), the browser is sending a GET request to the url, which is not allowed (response 405). I've tried also using axios({ method: 'post', ... })
but the same thing happens with the browser sending a GET request.
Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.
First we're passing the url of the service endpoint. Second we're passing object params which we created above and lastly we will pass headers to the post request. To pass raw data body content-type should be application/json.
First, it needs the URI of the service endpoint. Second, an object which contains the properties that we want to send to our server should be passed to it. Passing a data object as a parameter to the post method is optional; in this way, a post method is very similar to the get method.
Without question, some developers prefer Axios over built-in APIs for its ease of use. But many overestimate the need for such a library. The fetch() API is perfectly capable of reproducing the key features of Axios, and it has the added advantage of being readily available in all modern browsers.
Try to remove a trailing slash in COMMENTS_BASE_URL if you have it.
i.e. use '/resource'
instead of '/resource/'
. We had the same problem.
In my case, My server use https
so, http
=> https
then problem solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With