LoginSignup
0
1

More than 3 years have passed since last update.

【ajax】本番環境で謎の403エラー

Last updated at Posted at 2020-11-26

環境

  • Laravel 8系
  • PHP 7.4
  • appache

実装したいこと

ajaxを使用して非同期でPATCH処理を行いたい。

本題


$.ajax({
    type: 'PATCH',
    url: '/hoge_update',
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     },
     data: {
       id: id,
       item: item,
      },
      dataType: 'json',
 }).done(function (data) {

こんな感じで非同期でPATCH処理を実装しようと思ったら、本番環境で 403 Forbiddenエラーが。

調べてみるとどうやらapache側で許可されているのが GET POSTのみみたい(詳細はこちら

ただssh接続の許可がなかったり本番環境に入れない場合、confファイルを直接編集できないので、


$.ajax({
    type: 'POST', //POSTに変更
    url: '/hoge_update',
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     },
     data: {
       _method: 'PATCH', //PATCHを追加
       id: id,
       item: item,
      },
      dataType: 'json',
 }).done(function (data) {

だいぶ無理やりですがこれでしっかり200ステータスを返してくれました。

参考

0
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
0
1