简体   繁体   中英

Multiple Parameters to Laravel Route in Ajax Success

I want to create dynamic buttons in Laravel after Ajax Success Method. I have been able to do that as well. But the problem arises when I try to pass the route in the anchor tag. I am adding the full JavaScript code for ease of understanding:

var data = JSON.parse(data);
var finalData = '';
var search_body_data =  $("#searchTableBody");
for (i = 0; i < data.length; i++) {
     finalData = finalData + ` <tr> <td> ${data[i]['data_id']} </td> `;
     finalData = finalData + ` <td> ${data[i]['type']} </td> `;
     finalData = finalData + ` <td><a class="btn btn-primary btn-block" href="{{ route('notification_detail',['type'=>data[i]['type'],'doc_id'=>str_replace('/','-',data[i]['data_id']),'part'=>"0"]) }}">Detail</a></td> `;
     finalData = finalData + ` </tr> `;
}
search_body_data.html(finalData);

But this gives me the following error: Use of undefined constant data - assumed 'data' . I have also tried:

${'type'=>data[i]}

But it also gives the same error. Also tried some other variations but still no luck. Any work around will be highly appreciated.

Workaround would be to define your base route in a variable and append the parameters in the success callback function

var data = JSON.parse(data);
var finalData = '';
var base_url = "{{ url('notification_detail') }}";
var search_body_data =  $("#searchTableBody");
for (i = 0; i < data.length; i++) {
     finalData = finalData + ` <tr> <td> ${data[i]['data_id']} </td> `;
     finalData = finalData + ` <td> ${data[i]['type']} </td> `;
     finalData = finalData + ` <td><a class="btn btn-primary btn-block" href=" `+ 
                    base_url + `/`+ data[i]['type'] + `/` + 
                    data[i]['data_id'].replace('/', '-') +
                    `/0>Detail</a></td> `;
     finalData = finalData + ` </tr> `;
}
search_body_data.html(finalData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM