Display a Loader while processing ajax

Asked

Viewed 10,690 times

4

It can show a message like: "Loading..." while my ajax does the operation?

AJAX

              $.ajax({
                url: url,
                type: 'GET',
                dataType: 'json',                   
                success: function(data) {
                    console.log("sucesso");
                },
                error: function() {
                    console.log("erro");
                }   
              });

like, before entering the success: function(data){} show a downloader or an msg

2 answers

10

In addition to Silvio’s answer, you have yet another valid way of doing so, using the method beforeSend in the request:

<div id="divCorpo"></div>

$.ajax({
    url: url,
    type: 'GET',
    dataType: 'json',  
    beforeSend: function () {
        //Aqui adicionas o loader
        $("#divCorpo").html("<img src='imagem_gif_carregando.gif'>");
    },         
    success: function(data) {
       console.log("sucesso");
    },
    error: function() {
        console.log("erro");
    }   
 });
  • @Silvioandorinha, also take a look at this other solution :) However, your answer also responds well to the request!

  • 1

    Yes, really. I didn’t know that beforeSend is that as I always use the $.post instead of $.ajax I do it the first way, for no $.post has that beforeSend +1

6


You can do the following:

 $("div").html("<img src='imagem_gif_carregando.gif'>");
   $.ajax({
                url: url,
                type: 'GET',
                dataType: 'json',                   
                success: function(data) {
                    $("div").html("Requisição concluída");
                    console.log("sucesso");
                },
                error: function() {
                    console.log("erro");
                }   
              });

Placing an image inside a <div></div> before making the request

 $("div").html("<img src='imagem_gif_carregando.gif'>");

And after the request has finished removing the image of the div

$("div").html("Requisição concluída");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.