Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 ;}
        
        
        
        
Comment

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');
Comment

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";
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery smooth scrool 
Javascript :: how to get the first character of a string in javascript 
Javascript :: Unknown command: "create-react-app" 
Javascript :: react native italic text 
Javascript :: round till 2 digit in jquery 
Javascript :: javascript get month name 
Javascript :: node js delete folder with files 
Javascript :: js find lowest number in array 
Javascript :: setpresence discord.js 
Javascript :: update node .js 
Javascript :: install emailjs npm 
Javascript :: Codewars JS Beginner Series #3 Sum of Numbers 
Javascript :: Failed to transform react-native-reanimated-65.aar 
Javascript :: clear ctx canvas with javascript 
Javascript :: js get first space in string 
Javascript :: js date of yesterday 
Javascript :: jquery last character in string 
Javascript :: js append element to body 
Javascript :: javascript detect enter press on input 
Javascript :: jquery checkbox set checked 
Javascript :: jquery get each row in table 
Javascript :: get the first number of the integer in js 
Javascript :: align left text in reactstrap 
Javascript :: javascript change webpage title 
Javascript :: confirm delete message in jquery 
Javascript :: NullInjectorError: R3InjectorError(DashboardModule)[DatabaseService - DatabaseService - HttpClient 
Javascript :: jsonschema string enum 
Javascript :: install react with old version of node 
Javascript :: javascript remove from array by index 
Javascript :: rimraf node_modules 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =