Laravel - jquery ajax post method

  • STEPS

    STEP1: in html header tag

    
                          <meta name="csrf-token" content="lqtaEK8Q2DhbznWYTesWD04nYn4aBpyIxhm3L0qE" />
                          

    STEP2: in html part for button

    
                          <input type="text" name="name" class="form-control name" placeholder="Name" required="">
                          <input type="password" name="password" class="form-control password" placeholder="Password" required="">
                          <button class=" btn-submit">Submit</button>
                          

    STEP 3: button click event in jquery

    
                          //***  start button click event */
                          $(".btn-submit").click(function(e){
    
                          });
                          

    STEP 4: get form input fields value

    
                          $(".btn-submit").click(function(e){
    
                              //*********** get data from form fields */
                              var name = $(".name").val();
                              var password = $(".password").val();
                          });
                          

    STEP 5: send request to server using ajax

    
                          $(".btn-submit").click(function(e){
    
                              var name = $(".name").val();
                              var password = $(".password").val();
    
                              //*************  send request to server using ajax */
                              $.ajax({
                                  type:'POST',
                                  url:"{ route('ajaxRequest.post') }",                              
                                  success:function(data){
                                      
                                  }
                              });
                          });
                          

    STEP 6: send data to server using ajax

    
                          $(".btn-submit").click(function(e){
    
                              var name = $(".name").val();
                              var password = $(".password").val();
    
    
                              $.ajax({
                                  type:'POST',
                                  url:"{ route('ajaxRequest.post') }",  
                                  //** send data to server */
                                  data:{name:name, password:password},                            
                                  success:function(data){
                                      
                                  }
                              });
                          });
                          
    Note that data: parameter

    STEP 7: send csrf token

    
                          $(".btn-submit").click(function(e){
    
                              var name = $(".name").val();
                              var password = $(".password").val();
    
    
                              $.ajax({
                                  type:'POST',
                                  url:"{ route('ajaxRequest.post') }",  
                                  //** send csrf-token to server */
                                  headers: {
                                      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                  }
                                  data:{name:name, password:password},                            
                                  success:function(data){
                                      
                                  }
                              });
    
                          });
                          
    Note that header parameter