jQuery getJSON() async false

Today I searched jQuery getJSON() async false because I needed my jQuery Ajax call to be synchronous. It turns out that getJSON is just an Ajax shortcut in jQuery that can not have any extra parameters passed to it according to a post at Stackoverflow.com and then confirmed in the jQuery doc . So I had to turn my call back to normal jQuery.ajax().

//before
$.getJSON(url, data, function(json){
                                //success code
                           });

//after
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  async: false,
  success: function(json){
                  //success code
               }
});

As you can see the shortcut code is well, shorter, but not as flexible as the basic ajax function. Shortcuts are nice but shouldn’t be leaned on for heavy lifting.

6 Responses to jQuery getJSON() async false

  1. This is exactly, what I needed. Thanks!

    “As you can see the shortcut code is well, shorter, but not as flexible as the basic ajax function. Shortcuts are nice but shouldn’t be leaned on for heavy lifting.”

    So true!

  2. Stafford WIlliams

    Thanks for this, I was also after a synchronous getJSON(). Your solution (as expected due to getJSON docs) works a treat.

  3. Yes that will work if you want all of your future calls to be synchronous. Most of the time you only want a certain call to be synchronous in which this solution is better for.

  4. Thanks, exactly what I was looking for.

  5. $.ajaxSetup({async:false})

    $.getJSON(url,data ,function(response){

    //DO WHAT YOU NEED TO

    //ENABLE AGAIN FOR REST OF SYSTEM
    $.ajaxSetup({async:true})
    });

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Go back to top