181

I’ve been googling this and avoiding this error in my bug fix list for a long time now, but I’ve finally reached the end of the list, the last of which I have to make a function return true/false to state whether the validation has succeeded or not.

I'm using ajax to compare certain fields with those that are already in the db and by default the $.post() method does it's operations asynchronously.

I’m setting a variable inside the call onSuccess and the calling method doesn't get a response because of this, so all my js/jquery fails on pageLoad... I would prefer if I could still keep using the $.post method.

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
  • Ah, you'll need to add a code sample to clarify this one. – Femi Apr 28 '11 at 15:48
  • 4
    Do not do this. It will freeze the browser until the server replies. – SLaks Apr 28 '11 at 15:52
  • 1
    @Neal, the reason this question is unique is because jQuery has multiple ways of calling a server page and I specifically wanted to know how to make a $.post request synchronous, not just a jQuery ajax request – pythonian29033 Sep 26 '13 at 14:53

3 Answers3

225

jQuery < 1.8

May I suggest that you use $.ajax() instead of $.post() as it's much more customizable.

If you are calling $.post(), e.g., like this:

$.post( url, data, success, dataType );

You could turn it into its $.ajax() equivalent:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});

Please note the async:false at the end of the $.ajax() parameter object.

Here you have a full detail of the $.ajax() parameters: jQuery.ajax() – jQuery API Documentation.


jQuery >=1.8 "async:false" deprecation notice

jQuery >=1.8 won't block the UI during the http request, so we have to use a workaround to stop user interaction as long as the request is processed. For example:

  • use a plugin e.g. BlockUI;
  • manually add an overlay before calling $.ajax(), and then remove it when the AJAX .done() callback is called.

Please have a look at this answer for an example.

Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
18

If you want an synchronous request set the async property to false for the request. Check out the jQuery AJAX Doc

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
7

From the Jquery docs: you specify the async option to be false to get a synchronous Ajax request. Then your callback can set some data before your mother function proceeds.

Here's what your code would look like if changed as suggested:

beforecreate: function(node,targetNode,type,to) {
    jQuery.ajax({
         url:    url,
         success: function(result) {
                      if(result.isOk == false)
                          alert(result.message);
                  },
         async:   false
    });          
}

this is because $.ajax is the only request type that you can set the asynchronousity for

pythonian29033
  • 5,148
  • 5
  • 31
  • 56
  • 1
    This answer seems to be a copy of [this](http://stackoverflow.com/a/133327/3714134) answer. –  Dec 15 '15 at 09:28
  • 2
    @HugoZink It was definitely copied, but both of our answers were copied from the jQuery docs :-D – pythonian29033 Dec 23 '15 at 09:07