Solved

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

Posted on 2014-03-19
7
3,465 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 43

Expert Comment

by:Rob
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

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

Accepted Solution

by:
Rob 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 43

Expert Comment

by:Rob
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

828 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