I have created an image manager and I am loading each url with jquery, however, when I change the 'src' attributes in the ajax request, the image takes several seconds to appear in the browser, I have tried to place a spinner and wait To show everything once the request loads, but still, if I check the 'src' attribute the url is already loaded, but not the image, I have also tried to solve this problem by placing my request inside the jquery load method, but neither do I Works, what can be done in these cases?
$(window).on("load", function() {
$.ajax({
async:true,
cache:false,
dataType: 'json',
type: 'get',
url: 'cargarBanner',
success: function(data){
ajax_start();
if (data.success==true)
{
for(var i=0;i<data.banner.length;i++)
{
switch(data.banner[i].banner_type)
{
case "encabezado":
$("#"+data.banner[i].banner_type+" img").attr('src',flagsUrl+"/"+data.banner[i].banner_image);
break;
case "bsq-superior":
$("#"+data.banner[i].banner_type+" img").attr('src',flagsUrl+"/"+data.banner[i].banner_image);
break;
case "bsq-inferior":
$("#"+data.banner[i].banner_type+" img").attr('src',flagsUrl+"/"+data.banner[i].banner_image);
break;
}
}
}
},
complete: function(response) {
alert("listo");
ajax_stop();
},
//si ha ocurrido un error
error: function(data){
alert("ha ocurrido un error") ;
}
});
});`
****UPDATE**
I've changed my method to this way
public function cargarBannerT(Request $request)
{
$banner=$estados = DB::table("banners")->select('banner_image')->where('banner_type','=',$request->tipo)->get();
if (count($banner)>0) {
return $banner[0]->banner_image;
}
}
in this case, the method return the url of the image "1498501452.jpg", However, in the html, only plain text appears, and a broken image "src="../images/cargarBannerT?tipo=encabezado"