axios.post is sending a GET request

2019-06-15 09:45发布

问题:

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.

回答1:

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.



回答2:

In my case, My server use https

so, http => https

then problem solved.



回答3:

This happens then URL is redirected from POST call and GET happens after redirect. Most common redirects are caused by not needed trailing slashes or http redirecting to https



回答4:

In general this happens because i) the original POST request is redirected by the server for some reason and ii) chrome dev tools hides the initial POST request for some unknown reason in this case. Typical issues are:

  • Lack of authentication leading to a 302 redirect.
  • Use of http which the server redirects to https with a 301 or 302.

However there could be other reasons so you should inspect the HTTP requests going to your server to understand what's going on. A general tool like tcpflow is suitable. On nginx you can tail the access.log file. The idea is to understand what happens to the POST request so that the underlying issue can be corrected.