mercredi 12 juillet 2017

Call a delete url from routes using ajax?

How to call an url from routes using ajax ?

The route :

Route::delete('dashboard/tags/destroy/{tag}', 'TagCon@destroy')->name('tagdestroy');

Controller :

public function destroy($id) {
        $tag = Tag::findOrFail($id);
        $tag->delete();

        return response()->json($tag);
    }

Ajax code :

$(document).on('click', '.delete-modal', function () {
        $('.modal-title').text('Delete');
        $('#id_delete').val($(this).data('id'));
        $('#title_delete').val($(this).data('title'));
        $('#deleteModal').modal('show');
        id = $('#id_delete').val();
    });
    $('.modal-footer').on('click', '.delete', function () {
        $.ajax({
            type: 'DELETE',
            url: 'dashboard/tags/destroy/' + id,
            data: {
                '_token': $('input[name=_token]').val(),
            },
            success: function (data) {
                toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
                $('.item' + data['id']).remove();
                $('.col1').each(function (index) {
                    $(this).html(index + 1);
                });
            }
        });
    });

It does nothing maybe because it's not calling the correct route ? However, if I change this url: 'dashboard/tags/destroy/' + id, to this url: '' + id, it will return an error Missing required parameters for [Route: tagdestroy] [URI: dashboard/tags/destroy/{tag}]

Where is the wrong part with the code ? thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2uhG9nJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire