“Cara Mengirim Permintaan Ajax di Laravel” Kode Jawaban

Laravel kirim jax

// 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', '\App\Http\Controllers\Controller_name@name');
Ivan Cuaco

Cara Mengirim Permintaan Ajax di 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";
}
Isaac

Jawaban yang mirip dengan “Cara Mengirim Permintaan Ajax di Laravel”

Pertanyaan yang mirip dengan “Cara Mengirim Permintaan Ajax di Laravel”

Lebih banyak jawaban terkait untuk “Cara Mengirim Permintaan Ajax di Laravel” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya