经过长时间的研究,我尝试通过回调处理一些 AJAX 数据。我是意大利用户,所以如果我的解释不清楚,我深表歉意(实际上,我很难用英语解释我的问题)。
我正在尝试通过回调传递一些数据。我的 jQuery.ajax.async 没有设置为 false,因为我需要在异步模式下工作。因此,根据我在此处找到的一些帖子,我正在尝试将回调传递给我的函数。但是如果我尝试通过警报功能显示这些数据,我可以看到它是未定义的。这是代码:
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 返回的 JSON 代码示例(在 loadlastfiveevents 内)。
[
{
"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"
}
]