Is it possible to set async:false to $.getJSON call

JqueryAsynchronousGetjson

Jquery Problem Overview


Is it possible to set async: false when calling $.getJSON() so that the call blocks rather than being asynchronous?

Jquery Solutions


Solution 1 - Jquery

You need to make the call using $.ajax() to it synchronously, like this:

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

This would match currently using $.getJSON() like this:

$.getJSON(myUrl, myData, function(data) { 
  //stuff
  //...
});

Solution 2 - Jquery

Both answers are wrong. You can. You need to call

$.ajaxSetup({
async: false
});

before your json ajax call. And you can set it to true after call retuns ( if there are other usages of ajax on page if you want them async )

Solution 3 - Jquery

I think you both are right. The later answer works fine but its like setting a global option so you have to do the following:

    $.ajaxSetup({
		async: false
	});

    //ajax call here

    $.ajaxSetup({
		async: true
	});

Solution 4 - Jquery

In my case, Jay D is right. I have to add this before the call.

$.ajaxSetup({
    async: false
});

In my previous code, I have this:

var jsonData= (function() {
	var result;
	$.ajax({
		type:'GET',
		url:'data.txt',
		dataType:'json',
		async:false,
		success:function(data){
			result = data;
		}
	});
	return result;
})();
alert(JSON.stringify(jsonData));

It works find. Then I change to

var jsonData= (function() {
	var result;
    $.getJSON('data.txt', {}, function(data){
      result = data;
    });
	return result;
})();
alert(JSON.stringify(jsonData));

The alert is undefined.

If I add those three lines, the alert shows the data again.

$.ajaxSetup({
    async: false
});
var jsonData= (function() {
	var result;
    $.getJSON('data.txt', {}, function(data){
      result = data;
    });
	return result;
})();
alert(JSON.stringify(jsonData));

Solution 5 - Jquery

If you just need to await to avoid nesting code:

let json;
await new Promise(done => $.getJSON('https://***', async function (data) {
    json = data;
    done();
}));

Solution 6 - Jquery

I don't think you can set that option there. You will have to use jQuery.ajax() with the appropriate parameters (basically getJSON just wraps that call into an easier API, as well).

Solution 7 - Jquery

Roll your own e.g.

function syncJSON(i_url, callback) {
  $.ajax({
    type: "POST",
    async: false,
    url: i_url,
    contentType: "application/json",
    dataType: "json",
    success: function (msg) { callback(msg) },
    error: function (msg) { alert('error : ' + msg.d); }
  });
}

syncJSON("/pathToYourResouce", function (msg) {
   console.log(msg);
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionACPView Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryveljaView Answer on Stackoverflow
Solution 3 - JquerywebdevView Answer on Stackoverflow
Solution 4 - JqueryronrunView Answer on Stackoverflow
Solution 5 - Jqueryk06aView Answer on Stackoverflow
Solution 6 - JqueryDaffView Answer on Stackoverflow
Solution 7 - JqueryStonedecrozeView Answer on Stackoverflow