Solved

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

Posted on 2014-03-19
7
3,533 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link not working 6 42
Triple line in an alert message 4 24
Add somewhat of a wildcard to your javascript 12 30
Execute jQuery after Function 4 10
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

733 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