국가 별 정보를 업데이트하는 모달이 있습니다. 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
을하면 패치 경로가 선언 된 것을 볼 수 있습니다.
아이디어가 있으십니까?
편집 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);
편집 2 :
그래서 내 모달이 <input type="hidden" name="_token" value="{{{ csrf_token() }}}"/>
을 추가하고 나는 더 이상 405 오류가 발생하지
이 (의 일부) 내 컨트롤러 . 나는 여전히 내 JS의 "오류"부분을 얻는다. (단지 상태 0이된다.)
selector.attr ("action")의 모양을 알 수 있습니까? –
/country/idOrSomething –
과 같아야합니다. 그것은 국가/id' –