success : function not working laravel ajax

51 Views Asked by At

Laravel Ajax calling error but success function not working.

when the error call show load handling error. i am using this script:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type='text/javascript'>
$(function() {

// add new employee ajax request
$("#insertFile").submit(function(e) {
  e.preventDefault();
  const fd = new FormData(this);
  $("#addFile").text('Adding...');
  $.ajax({
    url: '{{ route('reg.store') }}',
    method: 'post',
    data: fd,
    cache: false,
    contentType: false,
    processData: false,
    dataType: 'json',
   
        error: function (){
            alert(e);
        }
  }).done(function(){
    if (results.status == "200") {
        
        Swal.fire(
          'Added!',
          'Employee Added Successfully!',
          'success'
        )
        fetchAllEmployees();
      }
        });
});

});
</script>
1

There are 1 best solutions below

0
Khang Tran On

It should be:

.done(function(data, status){
   if (status == "200") {
        Swal.fire(
          'Added!',
          'Employee Added Successfully!',
          'success'
        )
        fetchAllEmployees();
   }
});

The done() function is executed if the AJAX request succeeds. So you can consider removing if (status == "200"):

.done(function(){
     Swal.fire(
       'Added!',
       'Employee Added Successfully!',
       'success'
     )
     fetchAllEmployees();
});