Solved

using  beforeSend() in jQuery .ajax with animated gif

Posted on 2014-02-03
5
12,706 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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, …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now