Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a loading bar using jQuery while making a AJAX request

Tags:

jquery

ajax

I'm making a AJAX request with jquery like:

$.get('/Stuff.php', function (data) {
    $('#own').html(data);
});

while this data is loading I want to display a small text at the top of the page (which just says "loading...") without blocking it.

how to do this?

like image 671
gurehbgui Avatar asked Feb 06 '12 09:02

gurehbgui


People also ask

Can you use jQuery with AJAX?

What About jQuery and AJAX? jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Do AJAX requests reload the page?

AJAX is a developer's dream, because you can: Update a web page without reloading the page. Request data from a server - after the page has loaded. Receive data from a server - after the page has loaded.

What is jQuery AJAX request?

jQuery ajax() MethodThe ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Does AJAX request include cookies?

Basically, ajax request as well as synchronous request sends your document cookies automatically.


2 Answers

use the ajaxSetup

$.ajaxSetup({
    beforeSend:function(xmlHttpRequest){
    //show the loading div here
    },
    complete:function(){

    //remove the div here
    }
    });

now make the ajax call

$.get('/Stuff.php', function (data) {
    $('#own').html(data);
});
like image 181
Rafay Avatar answered Oct 06 '22 00:10

Rafay


$("#loading").show();   //before send
$.get('/Stuff.php', function (data) {
    $('#own').html(data);
    $("#loading").hide();  //when sucess
});
like image 26
Hadas Avatar answered Oct 06 '22 01:10

Hadas