Solved

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

Posted on 2014-03-19
7
3,693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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)

705 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