Home > Blockchain >  JS: Defining laravel route inside JS Function with parameter
JS: Defining laravel route inside JS Function with parameter

Time:10-08

I am going to send an AJAX request inside a Laravel Blade. This is my code:

function submitForm(userId) {
var ajaxUrl = '{!! route("user-role-update", ' userId ') !!}'
        $.ajax({
            type: 'PUT',
            url: ajaxUrl,
            data: {
                _token: "{!! csrf_token() !!}",
                'newRoles': userRoleTableData
            },

In the controller, I returned the userId to check if the userId is passed to the URL or not, but the result was not returning the userId:

{ 
  "msg": "Something wrong while deleting the old roles--",
  "id":" userId ",
  "isUserRoleSaved":false
}

Instead of returning the userId, it returns the variable "userId" with the " " from JS script. How can I solve this?

CodePudding user response:

function submitForm(userId) {
var url = '{{ route('user-role-update', ':userId') }}';
url = url.replace(':userId', userId);
    $.ajax({
        type: 'PUT',
        url: url,
        data: {
            _token: "{!! csrf_token() !!}",
            'newRoles': userRoleTableData
        },
  • Related