javascript - Laravel Patch AJAX -


i have modal updates information on countries.

 // partial code of 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> 

i'm trying implement ajax, if there errors, modal not close , error messaged appear under each input field.

this 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 {                 // debugging                 alert('data');              }         },         error: function (xhr, textstatus, thrownerror) {             alert(xhr.status);             alert(thrownerror);         }     }); }); 

i getting "405 method not allowed" error, although declared controller "ressource" this:

route::resource('country', 'countrycontroller',             ['except' => ['show']]); 

if php artisan route:list can see patch route declared.

any ideas?

edit 1:

this (part of) controller:

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:

so added <input type="hidden" name="_token" value="{{{ csrf_token() }}}"/> in modal , no longer 405 error. still have problem "error" part of js (only status 0)

type: 'patch' not exists on http methods not recognized laravel.
try this:

$.ajax({     type: 'post',     datatype: 'json',     url: selector.attr("action"),     data: {         '_method': 'patch',         'data': selector.serialize(),     }, 

you have submit method patch _method post data.

edit
controller function looks wrong. correct order

public function update(request $request, $id) 

instead of

public function update($id, request $request) 

ot: submitted addition laravel documentation gives hint problem rejected no comment.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -