Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a jQuery spinner image in an AJAX request

Tags:

json

jquery

ajax

I have an jQuery AJAX request which I want to have display an ajax spinner gif while the request loads and then disappear once the request is successful, can anyone suggest the best method to implement this into my jquery code below:

function updateCart( qty, rowid ){
$.ajax({
        type: "POST",
        url: "/cart/ajax_update_item",
        data: { rowid: rowid, qty: qty },
        dataType: 'json',
        success: function(data){                
            render_cart(data);
        }           
    });
}
like image 761
Zabs Avatar asked Oct 18 '13 09:10

Zabs


2 Answers

var $loading = $('#loadingDiv').hide();
$(document)
  .ajaxStart(function () {
    $loading.show();
  })
  .ajaxStop(function () {
    $loading.hide();
  });

where '#loadingDiv' is the spinner gif covering the part of the page or the complete page.

like image 108
Sachin Mour Avatar answered Sep 17 '22 15:09

Sachin Mour


  1. Get a loader gif from ajax loader (GIF images)
  2. Place this image where you want to show/hide.
  3. Before the ajax, show this image.
  4. Once completed, hide the image

function updateCart( qty, rowid ){
$('.loaderImage').show();
$.ajax({
        type: "POST",
        url: "/cart/ajax_update_item",
        data: { rowid: rowid, qty: qty },
        dataType: 'json',                         
        success: function(data){                
            render_cart(data);
            $('.loaderImage').hide();
        },
        error: function (response) {
           //Handle error
           $("#progressBar").hide();

    }           
    });
}
like image 39
Praveen Avatar answered Sep 19 '22 15:09

Praveen