Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show progress bar while loading, using ajax

I have a dropdown box. When the user selects a value from the dropdown box, it performs a query to retrieve the data from the database, and shows the results in the front end using ajax. It takes a little bit of time, so during this time, I want to show a progress bar. I have searched, and I have found numerous tutorials on creating progress bars for uploads, but I haven't liked any. Can anybody provides some helpful guidance for me?

My ajax code:

<script> $(function() {     $("#client").on("change", function() {       var clientid=$("#client").val();          $.ajax({             type:"post",             url:"clientnetworkpricelist/yourfile.php",             data:"title="+clientid,             success:function(data){              $("#result").html(data);             }         });      }); }); </script> 
like image 320
Xavi Avatar asked Nov 20 '13 11:11

Xavi


People also ask

How can I see the progress of AJAX call?

Use the ajaxStart to start your progress bar code. $(document). ajaxStop(function(){ // hide the progress bar $("#progressDialog"). modal('hide'); });

Why is AJAX success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

Does AJAX use Get or POST?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.


2 Answers

This link describes how you can add a progress event listener to the xhr object using jquery.

$.ajax({     xhr: function() {         var xhr = new window.XMLHttpRequest();          // Upload progress         xhr.upload.addEventListener("progress", function(evt){             if (evt.lengthComputable) {                 var percentComplete = evt.loaded / evt.total;                 //Do something with upload progress                 console.log(percentComplete);             }        }, false);                // Download progress        xhr.addEventListener("progress", function(evt){            if (evt.lengthComputable) {                var percentComplete = evt.loaded / evt.total;                // Do something with download progress                console.log(percentComplete);            }        }, false);                return xhr;     },     type: 'POST',     url: "/",     data: {},     success: function(data){         // Do something success-ish     } }); 
like image 193
Prateek Avatar answered Sep 19 '22 03:09

Prateek


<script> $(function() {     $("#client").on("change", function() {       var clientid=$("#client").val();      //show the loading div here     $.ajax({             type:"post",             url:"clientnetworkpricelist/yourfile.php",         data:"title="+clientid,         success:function(data){              $("#result").html(data);           //hide the loading div here         }     });      }); }); </script> 

Or you can also do this:

$(document).ajaxStart(function() {         // show loader on start         $("#loader").css("display","block");     }).ajaxSuccess(function() {         // hide loader on success         $("#loader").css("display","none");     }); 
like image 42
Suvash sarker Avatar answered Sep 21 '22 03:09

Suvash sarker