Reputation: 135
I am developing application which will get some dynamic content which is irrelevant to my question. and the question is
var pat;
$.post('venki/path.jsp', { nam:nam } , function(data) {
pat=data;
alert(pat); //it will displayed the received code form path.jsp
});
alert(pat);// it will not keep the data received from path.jsp
Now I need to not lose the scope.
For example:
var pat=0;
$.post(
pat = 1
);
alert(pat);
It should alert 1 and not o
In java, i should use static. In jquery, how to declare static variables.
Got an answer: Its simple and very useful and no need to worry about synchronous. the answer is tricky...
Upvotes: 0
Views: 264
Reputation: 82241
it is because post request is not completed when you are alerting pat value. To ensure that value is modified, alert it inside post success function:
var pat;
$.post('venki/path.jsp', { nam:nam } , function(data) {
pat=data;
alert(pat); //modified value
});
Upvotes: 4
Reputation: 853
If i'm not mistaken, the $.post
is async so the pat
is not losing it's scope but executed before the pat=data
executed
To make it synchronous call look at this question: how to make a jquery "$.post" request synchronous
Upvotes: 3