Advertisement
Briannn13

Laravel fix Csrf Token and sweet alert for delete

Jun 4th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.69 KB | Fixit | 0 0
  1.     <script>
  2.         $(document).ready(function(){
  3.             // csrf token
  4.             $.ajaxSetup({
  5.             headers: {
  6.                 'x-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  7.             }
  8.         });
  9.            
  10.             // sweet alert for delete
  11.             $('body').on('click', '.delete-item', function(e){
  12.                 e.preventDefault();
  13.                 let deleteUrl = $(this).attr('href');
  14.                 Swal.fire({
  15.                     title: "Are you sure?",
  16.                     text: "You won't be able to revert this!",
  17.                     icon: "warning",
  18.                     showCancelButton: true,
  19.                     confirmButtonColor: "#3085d6",
  20.                     cancelButtonColor: "#d33",
  21.                     confirmButtonText: "Yes, delete it!"
  22.                     }).then((result) => {
  23.                     if (result.isConfirmed) {
  24.                         $.ajax({
  25.                             type: 'DELETE',
  26.                             url: deleteUrl,
  27.                             success: function(data){
  28.                                 Swal.fire({
  29.                                 title: "Deleted!",
  30.                                 text: "Your file has been deleted.",
  31.                                 icon: "success"
  32.                                 });
  33.                                 window.location.reload();
  34.                             },
  35.                             error: function(xhr, status, error){
  36.                                 console.log(error);
  37.                             }
  38.                         })
  39.                        
  40.                      }
  41.                 });
  42.             })
  43.         })
  44.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement