I am creating an object in javascript:
var t = null;
$.getJSON('http://localhost:53227/Home/GetData', function (data) {
alert(data);
t = data;
});
alert(t);
When I alert data, I get an object back.
When I alert t, it’s null.
Can you please guide, how to set “t” to the returned data?
This will work as expected – the issue is not that
tis not set, it’s that you’re doingalert(t)before thegetJSONcallback is executed. Try doingalert(t)immediately aftert = data;In other words, here’s your current order of operations:
…as you can see, at step 3 ‘t’ will still be null. Try this instead:
Cheers