Storing data in datebase using jQuery Ajax Post mathod via RESTful Resource Controller in Laravel 5.2 -
i new laravel. i'm trying store data in form using jquery ajax (using post method). i'm using restful resources controller handle store request. when using form post method possible use route /student/store
.but how can route store method in controller in method. simplified code follows. needed.
("#btnsave").click(function(){ console.log("btnsave"); $.post("/student", { name: $("#studentname").val(), age: $("#studentage").val(), marks: $("#marks").val() },function(data, status){ alert("data: " + data + "\nstatus: " + status); }); });
with resource routes,
route::resource('student', 'studentcontroller');
the post
request routed store()
method default. can see url doing following in console:
php artisan route:list | grep student
if manually adding form fields ajax request, forgetting csrf token. add following inside form if hidden _token
field missing:
{{ csrf_field() }}
and update ajax request reflect new _token
field:
$.post("/student", { name: $("#studentname").val(), age: $("#studentage").val(), marks: $("#marks").val(), '_token': $('input[name="_token"]').val() }
Comments
Post a Comment