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

前端 未结 6 1357
迷失自我
迷失自我 2020-11-27 03:01

I want to return StudentId to use elsewhere outside of the scope of the $.getJSON()

j.getJSON(url, data, function(result)
         


        
6条回答
  •  广开言路
    2020-11-27 03:35

    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
    

提交回复
热议问题