2016-07-28 3 views
0

私は国に関する情報を更新するモーダルを持っています。LaravelパッチAJAX

// Partial code of the modal 
<div id="EditModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times; </button> 
       <h4 class="modal-title" id="myModalLabel">Edit <label id="EntityType"></label></h4> 
      </div> 

      <div class="modal-body"> 
       <div class="row"> 
        @yield('EditModalBody') 
       </div> 
      </div> 
      <div class="modal-footer" style="text-align: center"> 
       {{ Form::submit('Save', ['class' => 'btn btn-success', 'id' => 'editBtn']) }} 
       <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button> 
       {!! Form::close() !!} 
      </div> 
     </div> 
    </div> 
</div> 

私はエラーがある場合、モーダルが近いと、エラーがメッセージし、各入力フィールドの下に表示されないように、AJAXでこれを実装しようとしています。

これは私のJSです:

<script type="text/javascript"> 
$("#EditModal").submit(function (e) { 

    e.preventDefault(); 

    var selector = $(this); 

    $.ajax({ 
     type: 'PATCH', 
     dataType: 'json', 
     url: selector.attr("action"), 
     data: selector.serialize(), 

     success: function (data) { 
      if (data.success) { 
       alert('go go go'); 
      } else { 
       // for debugging 
       alert('data'); 

      } 
     }, 
     error: function (xhr, textStatus, thrownError) { 
      alert(xhr.status); 
      alert(thrownError); 
     } 
    }); 
}); 

私はこのような "ressource" としての私のコントローラを宣言したが、私は、 "405メソッド許可されていません" というエラーを取得しています:

Route::resource('country', 'CountryController', 
      ['except' => ['show']]); 

私がphp artisan route:listを実行すると、PATCHルートが宣言されていることがわかります。

アイデア?

EDIT 1:

これ(の一部)私のコントローラです:

public function update($id, Request $request) 
    { 
     $validator = Validator::make($request->all(), $this->getRules(), $this->getMesssages()); 

     if ($validator->fails()) { 
      $json = new stdClass(); 
      $json->success = false; 
      $json->errors = $Validator->errors(); 
     } 
     else { 
      $json = new stdClass(); 
      $json->success = true; 
     } 

     return Response::json($json); 

EDIT 2:

だから私は私のモーダルでこの<input type="hidden" name="_token" value="{{{ csrf_token() }}}"/>を添加していないと私はもはや405エラーが出ます。私はまだ私のJSの "エラー"部分を取得するという問題があります(ステータス0を取得するだけです)

+0

selector.attr( "action")の仕組みを教えてください。 –

+0

/country/idOrSomething –

+0

のようになります。それは国が/ id' –

答えて

0

type: 'PATCH'はHTTPメソッドには存在せず、したがってLaravelによって認識されません。
はこれを試してみてください:

$.ajax({ 
    type: 'POST', 
    dataType: 'json', 
    url: selector.attr("action"), 
    data: { 
     '_method': 'PATCH', 
     'data': selector.serialize(), 
    }, 

あなたは_methodポストデータとしてPATCH方法を提出する必要があります。


コントローラの機能が正しくありません。正しい順序は

public function update(Request $request, $id) 

代わりの

public function update($id, Request $request) 

OTのようになります。私はすでにあなたにこの問題についてのヒントを与えLaravelドキュメントの追加を提出したが、それはありませんコメントを拒否されました。

+0

これはいいですね!しかし、私はまだ "405メソッド..."を取得しています –

+0

正しいルート/ URLを使用してもよろしいですか?ポストデータ '_method = PATCH'を伴う' post'要求は、 'update'ルートによって受け入れられるべきです。 - https://laravel.com/docs/5.2/controllers#restful-resource-controllers – Kovah

+0

コントローラで 'dd($ request);'を実行すると、そのメソッドがPATCHとして正しく実行されていることがわかります。私は混乱しています... –