2010-03-24 17 views
28

Sé que hay miles de ejemplos en Internet, pero quiero que para el script ya tenga que mostrar una imagen gif mientras se recuperan los datos. Mi conocimiento de Java son pobres, por lo tanto lo que pido cómo cambiar lo siguiente:Mostrar la imagen de carga durante la publicación con ajax

<script type="text/javascript"> 
$(document).ready(function(){ 
    function getData(p){ 
    var page=p; 
    $.ajax({ 
     url: "loadData.php?id=<? echo $id; ?>", 
     type: "POST", 
     cache: false, 
     data: "&page="+ page, 
     success : function(html){ 
      $(".content").html(html); 
     } 
    }); 
} 
getData(1); 

$(".page").live("click", function(){ 
    var id = $(this).attr("class"); 
    getData(id.substr(8)); 
     }); 
     }); 
    </script> 

Y mi div está aquí:

<div class="content" id="data"></div> 

Gracias. John

Respuesta

61

Digamos que usted tiene un algún lugar etiqueta en la página que contiene el mensaje de carga:

<div id='loadingmessage' style='display:none'> 
    <img src='loadinggraphic.gif'/> 
</div> 

Se pueden añadir dos líneas a su llamada AJAX:

function getData(p){ 
    var page=p; 
    $('#loadingmessage').show(); // show the loading message. 
    $.ajax({ 
     url: "loadData.php?id=<? echo $id; ?>", 
     type: "POST", 
     cache: false, 
     data: "&page="+ page, 
     success : function(html){ 
      $(".content").html(html); 
      $('#loadingmessage').hide(); // hide the loading message 
     } 
    }); 
+5

Si la solicitud de Ajax no es exitosa, el gif de carga permanece visible en este caso. – ragatskynet

+1

En lugar de ocultar el div/gif en la devolución de llamada exitosa, agréguelo bajo el método always() (Este es el antiguo manejador de devolución de llamadas completo) – Narayana

9

Tome un vistazo a ajaxStart y ajaxStop

+0

Felipe, gracias por la sugerencia. Sin embargo, olvidé mencionar que mi conocimiento de js es pobre. Vi esas páginas pero no sé cómo cambiar mi script. –

9
$.ajax(
{ 
    type: 'post', 
    url: 'mail.php', 
    data: form.serialize(), 
    beforeSend: function() 
    { 
     $('.content').html('loading...'); 
    }, 
    success: function(data) 
    { 
     $('.content').html(data); 
    }, 
    error: function() 
    { 
     $('.content').html('error'); 
    } 
}); 

¡diviértete jugando!

si debe tener tiempos de carga rápidos que impidan que se muestre la carga, puede agregar un tiempo de espera de algún tipo.

2

Esto es muy simple y fácil de administrar.

jQuery(document).ready(function(){ 
jQuery("#search").click(function(){ 
    jQuery("#loader").show("slow"); 
    jQuery("#response_result").hide("slow"); 
    jQuery.post(siteurl+"/ajax.php?q="passyourdata, function(response){ 
     setTimeout("finishAjax('response_result', '"+escape(response)+"')", 850); 
      }); 
}); 

}) 
function finishAjax(id,response){ 
     jQuery("#loader").hide("slow"); 
     jQuery('#response_result').html(unescape(response)); 
     jQuery("#"+id).show("slow");  
     return true; 
} 
1
<div id="load" style="display:none"><img src="ajax-loader.gif"/></div> 

function getData(p){ 
     var page=p; 
     document.getElementById("load").style.display = "block"; // show the loading message. 
     $.ajax({ 
      url: "loadData.php?id=<? echo $id; ?>", 
      type: "POST", 
      cache: false, 
      data: "&page="+ page, 
      success : function(html){ 
       $(".content").html(html); 
     document.getElementById("load").style.display = "none"; 
      } 
     }); 
0

//$(document).ready(function(){ 
 
// \t $("a").click(function(event){ 
 
// \t \t event.preventDefault(); 
 
// \t \t $("div").html("This is prevent link..."); 
 
// \t }); 
 
//}); \t \t \t 
 

 
$(document).ready(function(){ 
 
\t $("a").click(function(event){ 
 
\t \t event.preventDefault(); 
 
\t \t $.ajax({ 
 
\t \t \t beforeSend: function(){ 
 
\t \t \t \t $('#text').html("<img src='ajax-loader.gif' /> Loading..."); 
 
\t \t \t }, 
 
\t \t \t success : function(){ 
 
\t \t \t \t setInterval(function(){ $('#text').load("cd_catalog.txt"); },1000); 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
\t \t 
 
<a href="http://www.wantyourhelp.com">[click to redirect][1]</a> 
 
<div id="text"></div>

-5

asegúrese de cambiar en ajax llamada

async: true, 
type: "GET", 
dataType: "html", 
+0

¿Por qué? ¿Cómo responde esto la pregunta? – Liam

Cuestiones relacionadas