2021年4月23日星期五

how to call delete route with ajax in laravel?

i want to call route resource with ajax, when i put type as delete i got error (exception "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException") and given message (message "The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.").function almost work beacuse after make refresh the status also change but i am the above error.

route :

        Route::resource('category', ArtistCategoryController::class);  

controller :

 public function destroy($id)  {      $category = ArtistCategory::findOrFail($id);      $deleteResponse = $category->update([         'status'=>'d'      ]);      if ($deleteResponse) {          deleteMessage();          return redirect()->back();      } else          errorMessage();    }  

view :

 <a href="" class="categoryDelete" data-id= ><i class="far fa-trash-alt" style="color: #b21f2d"></i></a>  

ajax:

<script>      $(".categoryDelete").on('click', function (e) {          e.preventDefault();          let id = $(this).data('id')          // console.log(id)          $.ajax({              url: '/artist/category/'+id,              type: 'DELETE',              data:{"id": id, "_token": ""},              success: function (data)              {                  alert(data)              },              error: function (e){                  alert(e)              }          });      })  </script>  
https://stackoverflow.com/questions/67238302/how-to-call-delete-route-with-ajax-in-laravel April 24, 2021 at 08:38AM

没有评论:

发表评论