Solved

Jquery $.unblockUI(); should  execute before alert is displayed on ajax success.

Posted on 2014-03-19
7
3,302 Views
Last Modified: 2014-03-21
hi,
i need to display processing message, i using blockui of jquery,everthing is working file except when the ajax call is successful alert is displayed at the same time processing is also displayed,wht i need is processing should be closed and alert should be displayed.

function SubmitFrm() {
	 {
		$.ajax({
			type : 'post',
			url : 'testsubmit',
			data : $('#registerform').serialize(),
			async : false,
			dataType : "text",
			beforeSend : function() {
				$.blockUI({ message: '<h1><img src="../img/loading.gif" /> Processing...</h1>'}); 
			},
			complete : function() {
				$.unblockUI();
			},
			success : function(data) {
			$.unblockUI();
				alert(data);
				
			},
			error : function(data) {
				// check status && error
			}

		});
	}

Open in new window

0
Comment
Question by:Rocking
7 Comments
 
LVL 42

Expert Comment

by:David S.
ID: 39941558
You can use setTimeout() to delay the alert long enough for the UI to be unblocked.
			success : function(data) {
				$.unblockUI();
				setTimeout(function(){
					alert(data);
				},50);				
			},

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39941965
You've also set async to false.  This should be set to true as indicated on the documentation page: http://malsup.com/jquery/block/
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 39942444
The unblockUI method has a callback method : onUnblock. Provide this as an option when calling unblockUI.
$.unblockUI( { onUnblock: function(data) { alert(data); }} );

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Rocking
ID: 39943135
@AlbertVanHalen
success : function(data) {
                  $.unblockUI( { onUnblock: function(data) { alert(data); }} )
                  },
Altert does not gets displayed.
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39944378
Here is a demo working:http://jsbin.com/femihobi/2/edit?html,js,console

To get it to work in the demo, I've changed a few variables that should work when changed back to your environment.

I changed "POST" TO "GET" as jsbin didn't allow for POST but it shouldn't matter
I changed the url to one I could test with

async however should stay as true

function SubmitFrm() {
    $.ajax({
      type : 'GET',
      url : '/rimow/1',
      data : $('#registerform').serialize(),
      async : true,
      dataType : "text",
      beforeSend : function() {
        $.blockUI({ 
          timeout: 0,
          message: '<h1><img src="../img/loading.gif" /> Processing...</h1>'
        }); 
      },
      complete : function() {
        //$.unblockUI();
      },
      success : function(data) {
        $.blockUI({
          onUnblock: function() {
            alert(data);
          }
        });
        $.unblockUI();
      },
      error : function(data) {
        // check status && error
        console.log("error");
        console.dir(data);
      }

    });
}

SubmitFrm();

Open in new window

0
 
LVL 42

Expert Comment

by:David S.
ID: 39944478
The unblockUI method has a callback method : onUnblock. Provide this as an option when calling unblockUI.
Ah. Yeah, that would be better than using setTimeout().
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39944808
Both the block and unblock have the same options but the scope is important, which is why the onUnblock must be defined in the success function to have access to the data object
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now