Solved

using  beforeSend() in jQuery .ajax with animated gif

Posted on 2014-02-03
5
13,758 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java script read and write file - save as location 8 42
Angular - code works but need to make a change 14 30
Javascript tutorials with real examples 5 25
Passport Expiry 3 44
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 Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

776 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