Reputation: 4998
I am trying to set a counter to my jquery ajax post function return. What I am trying to do is determine whether or not the back-end returns anything. So I am trying to count the times the each function is run, but after the post function has completed, i is set back to zero. But in the middle, i is functioning correctly. Why is this happening? And how do I check if a response is empty or not?
i=0;
$.post(url, data, function(resp) {
$('form').find('.errors').remove();
$.each(resp, function(index, value) {
$("#" + index).parent().append(getErrorHtml(value, index));
i++;
c.log(i);
});
}, 'json');
c.log(i);
Thanks!
Could making the Ajax request Sync help with this?
$.ajaxSetup({async:false});
Upvotes: 0
Views: 167
Reputation: 4998
The answer here is, that callbacks are not launched in line with the rest of the functions. Therefore, if something is defined outside of the function, and something is done to it in the function, and after the function, it is echoed, it is not sure, that these operations happened in that specific order.
And more closely to my question, I was trying to c.log()
before the callback executed.
Upvotes: 0
Reputation: 12437
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
Here data is response or output. You can check it
http://api.jquery.com/jQuery.post/
Upvotes: 1