Ajax delete action – undefined id

I wrote ajax request to delete data from db.

I think problem is, request can’t find ID.

Message:

No route found for "POST /app/delete/undefined"

I have method in my Controller:

/**
 * @Route("/app/delete/{id}", methods={"POST"}, name="delete", options={"expose"=true}, requirements={"id":"d+"})
 */
public function deleteAction(Request $request)
{
    $id = $request->get('id');
    $this->entityManager->remove($id);
    $this->entityManager->flush();
}

And Ajax method with opens a modal with confirmation button. On click it should delete specified entitiy.

 <a href data-toggle="modal" data-target="#confirm-delete" data-item_id="{{ item.id }}" class="btn btn-danger item_delete"></a>

And ajax request;

$(document).ready(function () {

$('#delete-btn').on('click', function () {
    $('.item_delete').attr('data-remove-url', removeUrl);
});

$(".remove-item").click(function () {
    var id = $(this).data("item_id");
    $.ajax({
        url: "/app/delete/"+id,
        type: 'POST',
        data: {},
        success: function () {
            $('#confirm-delete').fadeOut();
        },
        error: function (jqXHR) {
            $('div.modal-content').html(jqXHR.responseText)
        }
    });
});

});

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *