php - Update resource AJAX Laravel 5 -


i'm getting stuck ajax, dont know why ajax call returns error everytime. update method dont detect ajax request. i've never done ajax before, understand logic behind excuse ignorance if ask dumb things lol..

  route::post('edit/organisation/{id}',['uses' => 'organisationscontroller@update', 'as' => 'admin.organisation.update']); 

here controller method :

 public function update($route = null, $id, \illuminate\http\request $request)     {         $org = organisation::find($id)->first();         if($request->ajax())         {             dd('ok ajax');          }         if($org->update($request->all()))         {             //return redirect('admin/dashboard');             return redirect()->back();         }     } 

and here ajax

$(document).ready(function() { var form = $('.update_ajax'); form.bind('submit',function() {      $.ajax({        type: form.attr('method'),         url: form.attr('action'),         data: form.serialize(),         success: function(data)         {             alert(data);         },         error: function(data)         {             console.log('error');         }     });  }); }); 

and here html

  {!! form::model($org,['route' => ['admin.organisation.update',route::input('name'), $org->id],'class' => 'update_ajax' ]) !!}               {!! form::label('name','nom') !!}             {!! form::text('org_name',null,['class'=> 'form-control'])!!}             {!! form::label('org_type','type') !!}             {!! form::text('org_type',null,['class'=> 'form-control'])!!}             {!! form::label('siren','numéro de siren') !!}             {!! form::text('org_siren',null,['class'=> 'form-control']) !!}              {!! form::label('org_ape_naf','numéro ape naf') !!}             {!! form::text('org_ape_naf',null,['class'=> 'form-control']) !!}             {!! form::submit('appliquer',['class' => 'submit-data-btn']) !!}               {!! form::close() !!} 

you aren't preventing default happening when click submit button preventing ajax completing , forcing error method each time. explains why in php, $request->ajax() returning false.

form.bind('submit',function(e) {     e.preventdefault();     $.ajax({         type: form.attr('method'),         url: form.attr('action'),         data: form.serialize(),         success: function(data) {             console.log(data);         },         error: function(data) {             console.log('error');         }     }); }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -