我目前正在使用Ajax使用其API重复从应用程序中提取控制台日志。在PHP中执行返回值的var_dump()时,它是一个对象数组,我需要循环并拉取值。
这在PHP中当然很简单,但由于我对Javascript缺乏经验,我无法用for或foreach循环来解决这个问题。我在开发者控制台中使用了console.log,内容已经存在,但是有关如何循环使用的任何帮助都将不胜感激。
JS / Ajax的:
function getConsoleMessages()
{
var messageBox = document.getElementById("console_message");
// Clear the message box contents
//messageBox.value = '';
$.ajax({
type: "POST",
url: "ajax.php",
data: { 'action': 'getConsoleMessages' },
dataType: 'json',
success: function(data)
{
var messages = data['message'];
messages.forEach( function (item)
{
var x = item.Contents;
console.log(x);
});
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
}
});
};
PHP处理程序:
case "sendConsoleMessage":
{
$message = $_POST["message"];
if (empty($message))
{
$response['status'] = "failed";
$response['message'] = "Command parameter was not received.";
}
else
{
$amp->sendConsoleMessage($message);
$response['status'] = "success";
$response['message'] = $message;
}
echo json_encode($response);
break;
}
PHP var_dump:
object(stdClass)[2]
public 'result' =>
array (size=40)
0 =>
object(stdClass)[3]
public 'Timestamp' => string '/Date(1422419818830-0500)/' (length=26)
public 'Source' => string 'Console' (length=7)
public 'Type' => string 'Console' (length=7)
public 'Contents' => string 'Assigned anonymous gameserver Steam ID [A:1:721403909:5132].' (length=60)
1 =>
object(stdClass)[4]
public 'Timestamp' => string '/Date(1422419819038-0500)/' (length=26)
public 'Source' => string 'Console' (length=7)
public 'Type' => string 'Console' (length=7)
public 'Contents' => string 'VAC secure mode is activated.' (length=29)
2 =>
object(stdClass)[5]
public 'Timestamp' => string '/Date(1422419819145-0500)/' (length=26)
public 'Source' => string 'Console' (length=7)
public 'Type' => string 'Console' (length=7)
public 'Contents' => string 'tf_server_identity_account_id not set; not logging into registered account' (length=74)
答案 0 :(得分:1)
试试这个:
function getConsoleMessages()
{
var messageBox = document.getElementById("console_message");
// Clear the message box contents
//messageBox.value = '';
$.ajax({
type: "POST",
url: "ajax.php",
data: { 'action': 'getConsoleMessages' },
dataType: 'json',
success: function(data)
{
var messages = data['message']['result'];
messages.forEach( function (item)
{
var x = item.Contents;
console.log(x);
});
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
}
});
};
答案 1 :(得分:0)
从查看您的数据, 我想你想做标准
for(i;i<count;i++)
{
//then and this is the part I believe you are missing:
for (variable in object)
{ str += variable + ":" + object[variable] ; }
}
请参阅: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
答案 2 :(得分:0)
语法:
for ([start]; [condition]; [final-expression])
statement
规则:
Traditional way of iterating over arrays.
Can use var, but scope is always the complete surrounding function.
示例:
var arr = [ "a", "b", "c" ];
for(var i=0; i < arr.length; i++) {
console.log(arr[i]);
}