Solved

using  beforeSend() in jQuery .ajax with animated gif

Posted on 2014-02-03
5
13,233 Views
Last Modified: 2014-02-08
Hi,

How do I use the beforeSend() in a jQuery .ajax call so there is like an animated gif while waiting for the success return from .ajax call?

I want to load one of these animated gifs:
http://www.ajaxload.info/

Here's my code for the .ajax():
$.ajax({
        type : 'post',
        url: './model/change_paypal_price/modChangePaypalPrice.php',
        cache: false,
        dataType: 'json',
        data: {
            final_amount : final_amount,
            monthlypayentamt : monthlypayentamt,
            sPackageName : sPackageName,
            desire_payment_month : desire_payment_month
        },
        success: function(retData, textStatus, jqXHR) {
            var sEncryptedPPBtn = retData.encrypted_pp_button;

            if ( retData.success == 1 ) {
                //display variables in fields from database
                $('p#encypted_ppbtn').html(''); //empty html first
                $('p#encypted_ppbtn').html(sEncryptedPPBtn);               

                });

                console.log(textStatus+' success');
            } else { //fail. Either the data has not yet been saved (i.e. it is an initial save) or there was some other error with the sql call.
                //if (retData.email != '') {$('#passworderror').text('password error').show(500);}
                console.log('textStatus: '+textStatus+"\n'+'\nerror message: "+retData.error);
                //return false;
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log('textStatus: '+textStatus+'\n01_first_page.js call to 01_modGetFirstPageVars.php'+'\nerror message: '+retData.str_getfirstpagevars);
        }
    });

Open in new window


Thank you<><
0
Comment
Question by:Victor Kimura
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39831246
check this post :
http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_28300915.html

$.ajax({
     beforeSend:function() { 
         $("<img src='http://miniontours.yzi.me/loading.gif'  style='position:relative;top:200px;left:200px;z-index:2000' id='loading-excel' />").appendTo("body");
     },
     complete:function() {
           $("#loading-excel").remove();
     },
        type : 'post',
        url: './model/change_paypal_price/modChangePaypalPrice.php',
        cache: false,
        dataType: 'json',
        data: {
            final_amount : final_amount,
            monthlypayentamt : monthlypayentamt,
            sPackageName : sPackageName,
            desire_payment_month : desire_payment_month
        },
        success: function(retData, textStatus, jqXHR) {
            var sEncryptedPPBtn = retData.encrypted_pp_button;

            if ( retData.success == 1 ) {
                //display variables in fields from database
                $('p#encypted_ppbtn').html(''); //empty html first
                $('p#encypted_ppbtn').html(sEncryptedPPBtn);               

                });

                console.log(textStatus+' success');
            } else { //fail. Either the data has not yet been saved (i.e. it is an initial save) or there was some other error with the sql call.
                //if (retData.email != '') {$('#passworderror').text('password error').show(500);}
                console.log('textStatus: '+textStatus+"\n'+'\nerror message: "+retData.error);
                //return false;
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log('textStatus: '+textStatus+'\n01_first_page.js call to 01_modGetFirstPageVars.php'+'\nerror message: '+retData.str_getfirstpagevars);
        }
    });

Open in new window

0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 39831259
It is same as you are passing success and error callbacks.

Success:  function(){}'
beforeSend: function(){}
0
 

Assisted Solution

by:Victor Kimura
Victor Kimura earned 0 total points
ID: 39831315
@SriVaddadi,

I added this:

beforeSend: function() {
            $("#encypted_ppbtn").html("<img alt='Please wait...generating payment' src='https://secure.myultratrust.com/images/animated/ajax-loader.gif' />");
        },

Open in new window


I see the alt text display in FF. It doesn't work in chrome nor IE.

The image is uploaded to the server.

What's wrong?
0
 
LVL 16

Assisted Solution

by:SriVaddadi
SriVaddadi earned 500 total points
ID: 39831386
Try this

beforeSend: function() {
            $("#encypted_ppbtn").append("<img alt='Please wait...generating payment' src='https://secure.myultratrust.com/images/animated/ajax-loader.gif' />");
        },
0
 

Author Closing Comment

by:Victor Kimura
ID: 39843930
I found that the problem was my htaccess which prevent hotlinking! =)

thanks for the information!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now