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 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?