私は国に関する情報を更新するモーダルを持っています。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">× </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を取得するだけです)
selector.attr( "action")の仕組みを教えてください。 –
/country/idOrSomething –
のようになります。それは国が/ id' –