How can I return a variable from a $.getJSON function

it doesn’t seem to work the same way
c# does

To accomplish scoping similar to C#, disable async operations and set dataType to json:

var mydata = [];
$.ajax({
  url: 'data.php',
  async: false,
  dataType: 'json',
  success: function (json) {
    mydata = json.whatever;
  }
});

alert(mydata); // has value of json.whatever

Leave a Comment