Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to send ajax request in laravel

//script
<script>
  $(".buttonToClick").click(function(e){
      e.preventDefault();

      var name = $("input[name=name]").val();
      var email = $("input[name=email]").val();
      var message = $("input[name=message]").val();
      var _token   = $('meta[name="csrf-token"]').attr('content');

      $.ajax({
        url: "/ajax-request",
        type:"POST",
        data:{
          name:name,
          email:email,
          message:message,
          _token: _token
        },
        success:function(resp){
          alert(resp);
        }, error: function(){
          alert("Error");
        }
       });
  });
</script>

//route in web.php
	Route::post('/ajax-request', [yourControllerName::class, 'getAjaxRequest');
    
//Controller function
public function getAjaxRequest(Request $request){
	$data = $request->all();
    echo "Success";
}
 
PREVIOUS NEXT
Tagged: #send #ajax #request #laravel
ADD COMMENT
Topic
Name
1+3 =