“Laravel Ajax Post Request” Kode Jawaban

Laravel Ajax Post Request

            let route = "{{ route('srcname') }}";
            let token = "{{ csrf_token()}}";
            $.ajax({
                url: route,
                type: 'POST',
                data: {
                    _token:token,
                    name:'name',
                    email:'email'
                    phone:'09876767'
               
                },
                success: function(response) {
                    console.log(response)

                },
                error: function(xhr) {
                    //Do Something to handle error
                }});

        }
        
        
        
        
        // laravel web
        Route::post('srcname', [Controllername::class, 'filter'])->name('srcname');
        
        // Controller
         public function filter(Request $request){ return $request->name ;}
        
        
        
        
kazi web developer

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 “Laravel Ajax Post Request”

Pertanyaan yang mirip dengan “Laravel Ajax Post Request”

Lebih banyak jawaban terkait untuk “Laravel Ajax Post Request” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya