6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【本番環境では】405ルーティングエラー。原因はaxiosのurlだった

Posted at

○エラー内容・状況

The GET method is not supported for this route. Supported methods: POST.

ローカル環境では、同じコードでも問題なく動作していましたが、リリース環境になってから405エラーが出ました。

○解決方法

スクリーンショット 2021-09-23 8.23.36.png

原因は、

Vue.js側のaxiosでPOSTで記載しているが、GETになってしまっている事でした。

sample.vue
const url = '/post/search/';
$('.bath-search .error').text('');
axios.post(url, {
    prefecture: this.selectedPrefecture,
    keyword: this.keyword,
})

原因のコードがこちら

const url = '/post/search/';

最後のスラッシュがいらないです。

const url = '/post/search';

○スラッシュの有無で結果が変わる理由

末尾のスラッシュの有無で、アクセス対象がディレクトリかファイルかが変わってしまいます。

末尾スラッシュが入っている場合、web サーバ(Apache, nginxなど)はHTTPステータス 301 のリダイレクトレスポンスを返すことが多いです。このリダイレクト時にはもともとのリクエストが POST メソッドであっても強制的に GET メソッドとなり、リダイレクト元とは全く違う API を叩くことになりバグとなりえます。

○参考

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?