micky micky - 3 months ago 6
jQuery Question

Jquery image displaying

Jquery:

var res = $.parseJSON(data);

if(res.status == true)
{
//console.log(res.show.image);
var result = '<img src="{{asset('newuploads/+res.show.image+')}}"><br>'+
'Product Name:'+res.show.product_name+'<br>'+
'Price:'+res.show.price+'<br>';

$('#result').html(result);
}
}


Product Name and Price are displaying but not the image.
console.log(res.show.image);
gives the proper image name but i am not being able to display the image.

In php, (laravel) this would do it :

<img src="{{asset("newuploads/{$show->image}")}}">

Answer

{{asset()}} isn't meant to be used dynamically by javascript. Unless you're using handlebars and wrote your own helper, asset() just resolves as / in your browser, so let's just do that:

'<img src="/newuploads/' + res.show.image + '"/>'

Which will produce the same result that you're attempting to achieve.