<p>I want to return <code>StudentId</code> to use elsewhere outside of the <em>scope</em> of the <code>$.getJSON()</code></p>

<pre><code>j.getJSON(url, data, function(result)
{
    var studentId = result.Something;
});

//use studentId here
</code></pre>

<p>I would imagine this has to do with scoping, but it doesn't seem to work the same way <em>c#</em> does</p>

[{'answer_id': 31153, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 6, 'selected': True, 'text': "<p>Yeah, my previous answer does not work because I didn't pay any attention to your code. :)</p>\n\n<p>The problem is that the anonymous function is a callback function - i.e. getJSON is an async operation that will return at some indeterminate point in time, so even if the scope of the variable were outside of that anonymous function (i.e. a closure), it would not have the value you would think it should:</p>\n\n<pre><code>var studentId = null;\nj.getJSON(url, data, function(result)\n{\n    studentId = result.Something;\n});\n\n// studentId is still null right here, because this line \n// executes before the line that sets its value to result.Something\n</code></pre>\n\n<p>Any code that you want to execute with the value of studentId set by the getJSON call needs to happen either <strong>within</strong> that callback function or <strong>after</strong> the callback executes.</p>\n"}, {'answer_id': 31212, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': -1, 'selected': False, 'text': '<p>hmm, if you\'ve serialized an object with the <code>StudentId</code> property then I think that it will be:</p>\n\n<pre><code>var studentId;\nfunction(json) {\n    if (json.length &gt; 0)\n        studentId = json[0].StudentId;\n}\n</code></pre>\n\n<p>But if you\'re just returning the <code>StudentId</code> itself maybe it\'s:</p>\n\n<pre><code>var studentId;\nfunction(json) {\n    if (json.length &gt; 0)\n        studentId = json[0];\n}\n</code></pre>\n\n<p>Edit: Or maybe <code>.length</code> isn\'t even required (I\'ve only returned generic collections in JSON).</p>\n\n<p>Edit #2, this works, I just tested:</p>\n\n<pre><code>var studentId;\njQuery.getJSON(url, data, function(json) {\n    if (json)\n        studentId = json;\n});\n</code></pre>\n\n<p>Edit #3, here\'s the actual JS I used:</p>\n\n<pre><code>$.ajax({\n    type: "POST",\n    url: pageName + "/GetStudentTest",\n    contentType: "application/json; charset=utf-8",\n    dataType: "json",\n    data: "{id: \'" + someId + "\'}",\n    success: function(json) {\n        alert(json);\n    }\n});\n</code></pre>\n\n<p>And in the aspx.vb:</p>\n\n<pre><code>&lt;System.Web.Services.WebMethod()&gt; _\n&lt;System.Web.Script.Services.ScriptMethod()&gt; _\nPublic Shared Function GetStudentTest(ByVal id As String) As Integer\n    Return 42\nEnd Function\n</code></pre>\n'}, {'answer_id': 494328, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>If you wish delegate to other functions you can also extend jquery with the $.fn. notation like so:</p>\n\n<pre>\n<code>\nvar this.studentId = null;\n\n$.getJSON(url, data, \n    function(result){\n      $.fn.delegateJSONResult(result.Something);\n    }\n);\n\n$.fn.delegateJSONResult = function(something){\n  this.studentId = something;\n}\n\n</code>\n</pre>\n'}, {'answer_id': 1008786, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': "<blockquote>\n  <p>it doesn't seem to work the same way\n  c# does</p>\n</blockquote>\n\n<p>To accomplish scoping similar to C#, disable async operations and set dataType to json:</p>\n\n<pre><code>var mydata = [];\n$.ajax({\n  url: 'data.php',\n  async: false,\n  dataType: 'json',\n  success: function (json) {\n    mydata = json.whatever;\n  }\n});\n\nalert(mydata); // has value of json.whatever\n</code></pre>\n"}, {'answer_id': 15585054, 'author': 'bicycle', 'author_id': 1160952, 'author_profile': 'https://Stackoverflow.com/users/1160952', 'pm_score': 5, 'selected': False, 'text': '<p>Even simpler than all the above. As explained earlier <code>$.getJSON</code> executes async which causes the problem. Instead of refactoring all your code to the <code>$.ajax</code> method just insert the following in the top of your main .js file to disable the async behaviour:</p>\n\n<pre><code> $.ajaxSetup({\n   async: false\n });\n</code></pre>\n\n<p>good luck!</p>\n'}, {'answer_id': 44998393, 'author': 'Reinaldo Garcia', 'author_id': 6177779, 'author_profile': 'https://Stackoverflow.com/users/6177779', 'pm_score': -1, 'selected': False, 'text': "<pre><code>var context;\n$.ajax({\n  url: 'file.json',\n  async: false,\n  dataType: 'json',\n  success: function (json) {   \n    assignVariable(json);\n  }\n});\n\nfunction assignVariable(data) {\n  context = data;\n}\nalert(context);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/31129', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2993/']