The code you have shown is not indicating any issue with the execution.
Regarding the alerting of the values.
$.get()
is an asynchronous execution, meaning that code execution continues after .get()
has been kicked off.
The callback function you specified will not execute until the .get()
is completed but the rest of the code continues until then.
Hence, the first alert shows you the value before the callback is executed and as such prodList
is not populated yet.
The second alert then is displayed after the value is populated within the callback function.
$.get(url, function (model) {
prodList = $.parseJSON(model);
alert(prodList); // prodList is now populated
});
alert(prodList); // prodList here is alerted before it is populated
If you want to do something with prodList
you either use it within the callback function itself or from the callback function call another function, passing prodList
as an argument to process it there.