經過長期的研究,我試圖通過回調來處理一些AJAX數據。我是一個意大利用戶,所以如果我不清楚我的解釋,我很抱歉(實際上,我很難用英文解釋我的問題)。回調函數沒有獲取數據
我想通過回調傳遞一些數據。我的jQuery.ajax.async沒有設置爲false,因爲我需要以異步模式工作。所以我試圖給我的函數傳遞迴調,根據我在這裏找到的一些帖子。但是如果我試圖通過alert函數顯示這些數據,我可以看到它是未定義的。這裏是代碼:
function loadentities(id, username, password, system, host)
{
var postData = "username=" + encodeURI(username) +
"&password=" + encodeURI(password) +
"&system=" + encodeURI(system) +
"&host=" + encodeURI(host);
jQuery.ajax({
type: "POST",
dataType: "json",
data: postData,
beforeSend: function(x)
{
if(x && x.overrideMimeType)
{
x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: 'operations.php?op=loadentities',
error: function(x, textStatus, errorThrown)
{
alert("Request failed: " + textStatus + " " + errorThrown);
},
success: function(data)
{
var html = "";
for(var i=0; i < data.length; i++)
{
html += "<li><a onclick=\"showhide('" + data[i].entity + "');\">" + data[i].entity + "</a><ul id=\"" + data[i].entity + "\" class=\"list\"></ul></li>";
loadlastfiveevents(username, password, system, host, data[i].entity, function(data)
{
alert(data);
entityData = new Array();
for(var i = 0; i < data.length; i++)
{
var entityDataEntry = new Object();
entityDataEntry.key = data[i].key;
entityDataEntry.event = data[i].event;
entityDataEntry.timestamp = data[i].timestamp;
entityData.push(entityDataEntry);
}
alert(data[0].key);
});
entitiesValues[entitiesIndex++] = data[i].entity;
}
jQuery(html).appendTo('#hlentity' + id);
}
}); //end jQuery.ajax
}
function loadlastfiveevents(username, password, system, host, entity, buildlastfiveevents)
{
var postData = "username=" + encodeURI(username) +
"&password=" + encodeURI(password) +
"&system=" + encodeURI(system) +
"&host=" + encodeURI(host) +
"&entity=" + encodeURI(entity);
jQuery.ajax({
type: "POST",
dataType: "json",
data: postData,
beforeSend: function(x)
{
if(x && x.overrideMimeType)
{
x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: 'operations.php?op=getlatestevents',
error: function(x, textStatus, errorThrown)
{
alert("Request failed: " + textStatus + " " + errorThrown);
},
success: function(data)
{
var color = "green";
var html = "";
for(var i = 0; i < data.length; i++)
{
if(data[i].event == "SUP") color = "green";
else if(data[i].event == "IER") color = "yellow";
else if(data[i].event == "SDW") color = "black";
else color = "red";
html += "<li><img src=\"images/" + color + ".jpg\" />" + data[i].key + " " + data[i].event + " " + data[i].timestamp + "</li>";
}
jQuery(html).appendTo('#' + entity);
propagate(entity, data[0].event);
buildlastfiveelements(data);
}
}); //end jQuery
}
我希望這篇文章已經夠清楚了,謝謝你的耐心和時間。
編輯:這裏是由getlatestevents返回(loadlastfiveevents內)JSON代碼的樣本。
[
{
"key": "error",
"event": "SUP",
"timestamp": "2012-11-16 11:13:36"
},
{
"key": "error",
"event": "SDW",
"timestamp": "2012-11-16 11:12:57"
},
{
"key": "error",
"event": "SUP",
"timestamp": "2012-11-16 11:11:32"
},
{
"key": "error",
"event": "SDW",
"timestamp": "2012-11-15 19:40:31"
},
{
"key": "timeout",
"event": "SER",
"timestamp": "2012-11-15 19:30:54"
}
]
嘗試格式化您的代碼,它不容易閱讀(空格)。可以肯定的是,您指定了JSON數據類型,因此您的AJAX網址會返回JSON。對?否則它將無法工作 – sdespont
您在beforeSend時想要做什麼? – sdespont
@sdespont這是一條線,以確保正確處理返回的數據。 我與getlatestevents(loadlastfiveevents內)返回的JSON數據的樣本更新的問題 – shadow