Q123 Q123 - 4 years ago 121
jQuery Question

I do not see the value after ajax procedure

This is using jQuery ajax and get data from php file.

I can see data that I want to have at first and second alert however, I do not know why I cannot see any value from the third alert.

function getMarkerContent(marker) {
// Fetch data from database
var ret = "<ul>";
var temp = "";
var process = "";

$.get("../status.php", function (returnValue) {
var json = eval("(" + returnValue + ")");
$.each(json, function (key, state) {
obj = state;
ret += "<li>" + obj.member_id + ": " + obj.content + "</li>";
temp += obj.member_id + ": " + obj.content + "\n";

process = processfunction(temp);
alert(temp); // first alert
ret += "</ul>";

alert(process); // third alert
function processfunction(data) {

return data; // second alert

Answer Source

Congratulations! You just discovered why callbacks exist.


Your first and second alert are triggered within the .get success callback function, and thus are called only after the AJAX method returns a result.

Your third alert takes place outside of the callback, so it is fired before the AJAX method returns any data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download