Hello. Most recently, I started learning the laravel 5.3 framework, and the first difficulties arose. what to write to the url line in general: all the handlers in the DataController.php class, and how to let Ajax understand where they are processed?
1 answer
A simple example. In any view
<form action=""> {{csrf_field()}} <label>Введите Ваше имя</label> <input type="text" name="hello" id="hello"> <button id="text" type="button">push me</button> </form> <script> $( document ).ready(function() { $("#text").click(function(){ var n=$("#hello").val(); $.ajax({ url:'/ajaxtest', //url в роуте type:'POST', data:{mname: n}, success:function(data){ alert(data);//В случае успеха } }, error: function (data) { alert(data);//В случае ошибки } }); }); }); </script> In the route
Route::post('ajaxtest','Demo@save')->name('ajaxtest'); //ajaxtest дял url Demo-controler, @save функция In the controller
public function save(Request $post){ $hello="Привет".$post['mname']; return response()->json($hello); } Result alert Hi + your name
|