Reputation: 3051
I am making an ajax request where it may take much time to process the server-end.So I want to show a loading image at the time of request process.But loading image is not being shown while ajax requst.
var ref = createAjaxRequest();//Ajax req is created here...
if(ref){
showLoadingImg();
ref.open('POST','x.jsp',false);
ref.onreadystatechange = eventStateChange;
ref.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ref.setRequestHeader("Connection", "keep-alive");
ref.send();
}
else{ alert("Your browser does not support this feature");
}
function eventStateChange(){
if(ref.readyState==4 ){
//processing response here......
hideLoadingImg();
}
}
function showLoadingImg();{
/* <div id="ajaxLoading">(with background image is in page)
It is displayed properly when I set display:inline
manually through developer tools of a browser.</div>
*/
document.getElementById('ajaxLoading').style.display='inline';
}
function hideLoadingImg();{
document.getElementById('ajaxLoading').style.display='none';
}
is there anything wrong?
I tried to debug and found that:
Though showLoadingImg()
is called before open()
method, the loading image is displayed on browser only after ref.readyState==2
.
But unfortunately time gap between readyState==2
and readyState==4
is very less, the loading image is immediately hidden.
Thus user cannot see the loading image...
So, what I am doubting is, doesn't ajax run the script unless it goes to readyState==2
.
Upvotes: 0
Views: 248
Reputation: 26
I thinks your call to open
is wrong.
The third argument (boolean) indicates if the request is asynchronous or not.
Consider complete documentation here : http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
ref.open('POST','x.jsp',true);
Should solve your problem.
Regards
Upvotes: 1
Reputation: 943207
XMLHttpRequest blocks if you set async
to false
as you do with the third argument here: ref.open('POST','x.jsp',false);
.
Upvotes: 2