How to show spinner on ajax call
Posted By : Sudhir Kumar Ojha | 31-Oct-2017
Ajax
also knew as synchronous JavaScript and XML.It is a set of Web development techniques using many Web technologies on the client side to create asynchronous Web applications. With Ajax, Web applications can send data to and retrieve from a server asynchronously (in the background) without interfering with the display and behavior of the existing page.And spinner will help you to feel that some action going on in the background.
The following steps are taken to create a spinner while ajax call is going on.
1). Create a spinner image or load it from ajax loader.
2). Place the image in our code where you want to show.
3). Before doing an ajax call show the image using jquery .show() method.
4). Once ajax call is completed hide the spinner using .hide() method.
Now I will show how to integrate above steps into your code.
<div id="spinner">
<img src="someimageurl.jpg" />
</div>
<a onclick="updateStudent('stud123','john')"></a>
Now create a javascript method that will do an ajax call.Please consider the following.
function updateStudent( studentId,name )
{
$('#spinner').show();
$.ajax({
type: "POST",
url: "/student/studentupdate",
data: { studentId: studentId, name: name },
dataType: 'json',
success: function(data)
{
alert("success");
$('#spinner').hide();
},
error: function (response)
{
$("#spinner").hide();
}
});
}
Regards
Sudhir Ojha
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sudhir Kumar Ojha
Sudhir Kumar Ojha is having skills to work as Software developer & having good knowledge of Java.