Stack Overflow Asked by crapcoder on January 3, 2022
so to quickly explain if i change myObj from = array
to myObj = { the content of the callback.txt file }
then this works. But when i try to call the contents into the var it doesn’t when i call array into the innerHTML i see the full contents on the page. So I know the file is loading correctly, however I am unable to get it to parse the file for the information I want or to run it as an array.
the array file as
{ "information": [
{
"Telephone # Dialed": "8555551234",
"Employee ID": "XYZ456",
"IncidentID": "INC000022222226",
"Domain": "CORP",
"Tier": "903",
"HierarchyCode": "HACA4564S",
"First Name": "Jane",
"Last Name": "Smith",
"City": "NORTH LAS VEGAS",
"State": "NV",
"Office Phone": "1234",
"CallbackNumber": "4567",
"Callback Successful": "N/A",
"Callback Attempts": "0"
},
{
"Telephone # Dialed": "8555551234",
"Employee ID": "XYZ456",
"IncidentID": "INC000022222228",
"Domain": "CORP",
"Tier": "903",
"HierarchyCode": "HACA4564S",
"First Name": "John",
"Last Name": "Smith",
"City": "NORTH LAS VEGAS",
"State": "NV",
"Office Phone": "555",
"CallbackNumber": "1234",
"Callback Successful": "N/A",
"Callback Attempts": "0"
}
]
}
and the JavaScript as
var array = [];
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var text = xmlhttp.responseText;
array = text.split(/n|r/g);
var obj = JSON.parse(text);
array = obj.information;
var listener = document.getElementById("submitThis");
listener.onclick = function() {
var incident = document.getElementById("incident").value;
var myObj, i, j, x = "";
myObj = array;
for (i in myObj.information) {
if (myObj.information[i].IncidentID == incident) {
for (j in myObj.information[i].CallbackNumber) {
x = myObj.information[i].CallbackNumber;
}
}
}
document.getElementById("test").innerHTML = x;
}
}
}
xmlhttp.open("GET", "callback.txt", true);
xmlhttp.send();
<html>
<body>
<p id="test"></p>
<input type="text" id="incident">
<input type="button" id="submitThis" value="make it happen">
</body>
</html>
Your problem is that you are trying to access a object inside the array that isn't there (you already called that earlier in your code).
<html>
<body>
<p id="test"></p>
<input type="text" id="incident">
<input type="button" id="submitThis" value="make it happen">
<script>
var array = [];
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var text = xmlhttp.responseText;
//array = text.split(/n|r/g);
var obj = JSON.parse(text);
array = obj.information;
var listener = document.getElementById("submitThis");
listener.onclick = function() {
var incident = document.getElementById("incident").value;
var myObj, i, j, x = "";
myObj = array ;
for (i in myObj) {
if ( myObj[i].IncidentID == incident ) {
for (j in myObj[i].CallbackNumber) {
x = myObj[i].CallbackNumber ;
}
}
}
document.getElementById("test").innerHTML = x;
}
}
}
xmlhttp.open("GET", "callback.txt", true);
xmlhttp.send();
</script>
</body>
</html>
At array = obj.information;
you already access information. Then you try to get it again in your for loop and the code inside the for loop (ie: for (i in myObj.information) {
).
Also I commented out array = text.split(/n|r/g);
because it wasn't doing anything in your code. And you don't want to split the JSON input before running JSON.parse
anyway.
I hope this helps to clarify the problem.
Answered by Fullslack on January 3, 2022
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP