3

I would like to display loading indicator while data is loading. below is my current codes:

$.ajax({
     type: "POST",
     url: "URL",
     data: dataString,
     cache: false,
     success: function(html) {
         $("#div").html(html);
     }
});

What should I do?

2

5 Answers 5

2

use this:

$.ajax({
     type: "POST",
     url: "URL",
     data: dataString,
     cache: false,
     success: function(html) {
         $(selector).html();
         $("#div").html(html);
     },
     beforeSend: function(){
         $(selector).html("your loading image");
     }

});
$(selector).html();
Sign up to request clarification or add additional context in comments.

Comments

2

You may use jquery ajaxStart and ajaxStop utilities for this.

like

$( document ).ajaxStart(function() {
  $( "#loading" ).show();
});

Comments

1
<script type="text/javascript">
$(document).ready(function() {  

$.ajax({
     type: "POST",
     url: "URL",
     data: dataString,
     cache: false,
     success: function(html) {
         $("#div").html(html);
     }
});

$( document ).ajaxStart(function() {
  $("#loading" ).show();
});

$( document ).ajaxStop(function() {
  $("#loading" ).hide();
});


});
 </script>

Comments

0
$.ajax({
     type: "POST",
      beforeSend : function(){ $('#somediv').show();},
     url: "URL",
     data: dataString,
     cache: false,
     success: function(html) {
      $("#div").html(html);
      $("#somediv").hide();
     }
});

2 Comments

This will give the error at line: beforeSend : $('#somediv').show(); there should be comma not semicolumn.
It should be beforeSend : function(){ $('#somediv').show();}, instead of beforeSend : $('#somediv').show();
0

Here is a simple logic.

1) Before the ajax call, show the loading gif image.

2) on callback, hide the loading image.

$("#loading" ).show();
$.ajax({
     type: "POST",
     url: "URL",
     data: dataString,
     cache: false,
     success: function(html) {
         $("#div").html(html);
         $("#loading" ).hide();
     }
     error: function (html) {
         console.log("errorResponse: " + html);
         $("#loading" ).hide();
     }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.