Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using  beforeSend() in jQuery .ajax with animated gif

Posted on 2014-02-03
5
Medium Priority
?
18,144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

704 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