I am currently using Ajax to repeatedly pull console logs from an application using its API. When doing a var_dump() of the return values in PHP, it is an array of objects which I will need to loop through and pull the values.
This is of course simple in PHP but as inexperienced with Javascript as I am, I cannot figure this out with for or foreach loops. I have used console.log with Developer Console and the contents are there but any help on how to loop through this would be appreciated.
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 Handler:
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)