Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel send ajax

// sending ajax from view to controller
<script>
let data = {
	_token: "{{ csrf_token() }}", //required to use token if method is post
	name: "ajax",
    request: 1
}
$.ajax({
	type: "post",
    url: "{{ URL::to('/') }}/ajax_request_url",
    data: data,
    success: function(response) {
		console.log(response);
    }
});
</script>


// controller
class Controller_name extends Controller {
	function name(Request $request) {
  		$post = $request->input();	
  		header('Content-Type: Application/json');
  		echo json_encode($post);
	}
}

// web.php
route::any('/ajax_request_url', 'AppHttpControllersController_name@name');
 
PREVIOUS NEXT
Tagged: #laravel #send #ajax
ADD COMMENT
Topic
Name
9+8 =