Something strange is happening... the code snippet below generates a table displaying a list of SNMP object/values from any OID provided for walking. Strangely, the variable 'jason' is not behaving as expected.
Initially, I am unable to access the 'jason' object from the Chrome console using console.log or console.dir.
Furthermore, when a console.log(key) is placed within the 'for in' loop, nothing is outputted.
Moreover, when attempting to access the properties of 'jason' using dot notation within the for loop, 'undefined' is returned.
Lastly, despite reading on other Stack posts that an array-like object should not utilize the 'for in' loop, changing it to a regular for loop causes it to malfunction. It seems like the 'jason' variable is invisible, even though the script below functions correctly. Could it be a fundamental misunderstanding of the JavaScript language on my part? Any insights would be greatly appreciated.
document.getElementById('submit').addEventListener('click', function(){
document.getElementById('msg').innerHTML = '<br /><button class="btn btn-success" disabled="disabled"> <i class="fa fa-spinner fa-spin"></i> </button> Loading...';
request = new XMLHttpRequest();
var host = document.getElementById('host').value,
comm = document.getElementById('comm').value || 'public',
oid = document.getElementById('oid').value || '1.3.6.1.2.1.2.2';
var post_vars = 'host=' + encodeURIComponent(host);
post_vars += '&comm=' + encodeURIComponent(comm);
post_vars += '&oid=' + encodeURIComponent(oid);
request.open('POST', 'snmp_json.php');
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.addEventListener('readystatechange', function() {
if (request.readyState === 4 && request.status === 200) {
var jason = JSON.parse(request.responseText);
var output = '<table class="table table-hover">';
output += '<thead><tr><th>Object</th><th>Value</th></tr></thead>';
output += '<tbody>';
for (key in jason) {
output += '<tr><td>' + key + '</td>';
output += '<td>' + jason[key] + '</td></tr>';
}
output += '</tbody></table>';
document.getElementById('msg').innerHTML = output;
};
});
request.send(post_vars);
});