0

I have this JavaScript code:

<script type="text/javascript">
  $(document).ready(function() {
      $(".deleteImage").on('click', function() {
          var idmess = $(this).data("id");
          var token = $(this).data("token");
          $.ajax({
              url: '{{ url('admin/deletemulti') }}/'+encodeURI(idmess),
              type: 'DELETE',
              dataType: "JSON",
              data: {
                  "id": idmess,
                  "_method": 'DELETE',
                  "_token": token,
              },
              success:function() {
                alert("it Work");
              }
          });
      });
  });
</script>

is working just fine (data is removing from my DB and I get 200 in network), except I cannot get my alert any idea why is that?

UPDATE

my network

my network response

Delete Function

function destroy(Request $request) {
    $image = Image::find($request->id);
    Storage::delete($image->name);
    $image->delete();
  }
mafortis
  • 6,750
  • 23
  • 130
  • 288

3 Answers3

1

You have used ajax with dataType : json
So you need to respond with a valid JSON as HttpResponse else it gets into a error event.

The response of your api call:

{{ url('admin/deletemulti') }}/'+encodeURI(idmess)

should be a valid JSON. Please check api response value and fix it, or share it so that we can help you update that.

In case the response is not a valid JSON, success function will never get triggered and hence alert is not getting executed.

More info :

Ajax request returns 200 OK, but an error event is fired instead of success

abhinsit
  • 3,214
  • 4
  • 21
  • 26
1

try passing an argument in your success function.

success:function(data) {
   alert("it Work");
}
user9452884
  • 275
  • 1
  • 4
  • 13
0

When I'm making backend for myown use, and when I'm writing json, I usually put my json on success like session = true, or something like that, so later you can check like :

success: function(json) {
 if(json.session == true) {
   alert('something')
 }
}

Maybe it's not the best solution, but it's working perfectly for me.