首页 > 解决方案 > 如何在 javascript 中以 json 形式显示来自 laravel 控制器的图像

问题描述

我的代码中有数据中的图像

$(document).ready(function(){   

    $('.show-btn').click(function() {
        var pid = $(this).data("id");
        //alert(pid);
         $.ajax({
               type:'get',
               url:'home/dataajex/'+pid,
               dataType:"json",
               success:function(data) { 
               data.forEach(function(element) {  
                    var imgSource = document.getElementById('data').src ;

                    document.body.appendChild(load_data); 
                    console.log(imgSource);     
                });
                console.log(data);

               }
            });                      
    });
});

我如何在其中显示图像我的 html div id 是 load_data

标签: javascriptlaravel

解决方案


尝试这个,

$.ajax({
           type:'get',
           url:'home/dataajex/'+pid,
           dataType:"json",
           success:function(data) { 

             $.each(data, function(index) {
                 $('#yourDivId').append("<img src='"+data[index]+"'>");
             }

           }
        });  

推荐阅读