doufu3718 2017-06-10 04:22
浏览 91
已采纳

Ajax成功中Laravel路由的多个参数

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.

  • 写回答

1条回答 默认 最新

  • dongque20030402 2017-06-10 04:37
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程