mafortis
mafortis

Reputation: 7118

javascript not show results on success

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();
  }

Upvotes: 0

Views: 56

Answers (3)

user9452884
user9452884

Reputation: 283

try passing an argument in your success function.

success:function(data) {
   alert("it Work");
}

Upvotes: 1

Milos Antic
Milos Antic

Reputation: 1

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.

Upvotes: 0

abhinsit
abhinsit

Reputation: 3272

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

Upvotes: 1

Related Questions