Probably an async issue - any code after the $.getJSON call (but not in the
callback) will get executed before the async call returns. Any code that is
dependent on this.Settings being set with the returned data must be within
the callback function.
Also "this" is probably not what you are expecting it to be. Do an alert or
console.log with "this" in your callback function to make sure it's what
you're expecting.
-- Josh
-Original Message-
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Ido Yehieli
Sent: Monday, January 04, 2010 9:55 AM
To: jQuery (English)
Subject: [jQuery] Callback in $.getJSON is called but variable assignment
doesn't persist.
Hi,
on the following code:
$.getJSON("http://www.tametick.com/test2/json/settings.json",function
(data) {
this.Settings = data;
alert(data);
alert(this.Settings);
});
which can be found on http://www.tametick.com/test2 (line 63 in
http://www.tametick.com/test2/js/cardinal-quest.js), I am getting the
alerts correctly (returning the object read from the file) but after
the function ends this.Settings revurts back to null and I get the
error:
Error: Settings is undefined
Source File: http://www.tametick.com/test2/js/cardinal-quest.js
Line: 70
Any idea what is going wrong?
Thanks,
Ido.