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

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

 j.getJSON(url, data, function(result) { var studentId = result.Something; }); //use studentId here 

I would suggest that this is related to scoping, but it seems like this is not the way C # does

+44
javascript scope jquery ajax return-value
Aug 27 '08 at 20:14
source share
6 answers

Yes, my previous answer does not work, because I did not pay attention to your code. :)

The problem is that the anonymous function is a callback function, i.e. getJSON is an asynchronous operation that will return at some indefinite point in time, so even if the scope of the variable was outside this anonymous function (i.e. closure), it would not have a value that you would think about:

 var studentId = null; j.getJSON(url, data, function(result) { studentId = result.Something; }); // studentId is still null right here, because this line // executes before the line that sets its value to result.Something 

Any code that you want to execute with the studentId value set by the getJSON call must be executed either internally , that the callback function, or after it executes the callback. p>

+36
Aug 27 '08 at 20:19
source share

it looks like it works the same C # does

To perform a review 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 
+59
Jun 17 '09 at 18:28
source share

Even simpler than all the above. As explained earlier, $.getJSON executes async, which causes the problem. Instead of rearranging all your code into the $.ajax method, simply paste the following at the top of your main .js file to disable async behavior:

  $.ajaxSetup({ async: false }); 

Good luck

+14
Mar 23 '13 at 9:07
source share

If you want to delegate other functions, you can also extend jquery with $ .fn. designation like this:

 var this.studentId = null; $.getJSON(url, data, function(result){ $.fn.delegateJSONResult(result.Something); } ); $.fn.delegateJSONResult = function(something){ this.studentId = something; }
var this.studentId = null; $.getJSON(url, data, function(result){ $.fn.delegateJSONResult(result.Something); } ); $.fn.delegateJSONResult = function(something){ this.studentId = something; } 
+2
Jan 30 '09 at 2:48
source share
 var context; $.ajax({ url: 'file.json', async: false, dataType: 'json', success: function (json) { assignVariable(json); } }); function assignVariable(data) { context = data; } alert(context); 
-one
Jul 09 '17 at 16:08
source share

hmm, if you serialized an object with the StudentId property, then I think it will be:

 var studentId; function(json) { if (json.length > 0) studentId = json[0].StudentId; } 

But if you just return StudentId , perhaps this:

 var studentId; function(json) { if (json.length > 0) studentId = json[0]; } 

Edit: Or maybe .length is not even required (I just returned the generic collections to JSON).

Edit # 2, it works, I just tested:

 var studentId; jQuery.getJSON(url, data, function(json) { if (json) studentId = json; }); 

Change # 3, the actual JS is used here:

 $.ajax({ type: "POST", url: pageName + "/GetStudentTest", contentType: "application/json; charset=utf-8", dataType: "json", data: "{id: '" + someId + "'}", success: function(json) { alert(json); } }); 

And in aspx.vb:

 <System.Web.Services.WebMethod()> _ <System.Web.Script.Services.ScriptMethod()> _ Public Shared Function GetStudentTest(ByVal id As String) As Integer Return 42 End Function 
-2
Aug 27 '08 at 20:38
source share



All Articles