[Solved] How to use route in Javascript


You can use the route helper with string placeholders and then replace the placeholder with javascript variables.

function AddFavourites(productid, userid) {
    let url = "{{ route('product.like', ['id' => ':id']) }}".replace(':id', productid);

    $.ajax({
        method: 'post',
        url: url,
        data: {
            'user_id': userid,
        },
    }).done(function(response, status){
        //
    }).fail(function(jqXHR, textStatus, errorThrown){
        //
    });
}

0

solved How to use route in Javascript