I know this post is old(er), but I had this same problem and thought I'd post the solution for the next guy. Basically, like bfalvretto says above, jquery's .get() and .post() functions are async, so by the time it got the remote url, the variable's value was different.
My solution was to throw the .get() part of it into it's own separate function to limit the scope of the callback.
This is what I came up with:
$("#SomethingClick").click(function(e) {
e.preventDefault();
$.post("..getURL", { id: 123 }, function(data) {
for (i = 0; i < data.rows.length; i++) {
ExternalFunction(data.rows[i].Value1, data.rows[i].Value2);
}
});
});
...
function ExternalFunction(value1, value2) {
$.get("/Core/ContactViewer/" + contactID, function (data2) {
$("someoutputdiv").html("<div>" + value1 + ": " + value2 + "</div>");
});
}
Hope this helps.