Javascript从文本文件加载数组



以便快速解释我是否将myObj from = array更改为myObj = { the content of the callback.txt file }那么这就行了。但是,当我试图将内容调用到var中时,当我将数组调用到innerHTML中时,我不会看到页面上的全部内容。因此,我知道文件加载正确,但我无法让它解析文件以获取我想要的信息,也无法将其作为数组运行。

数组文件作为

{ "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"
}
]
}

以及作为的JavaScript

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>

您的问题是试图访问数组中不存在的对象(您在代码的早期已经调用了该对象(。

<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>

array = obj.information;,您已经访问信息。然后,您尝试在for循环中再次获取它,并在for循环(即:for (i in myObj.information) {(中获取代码。

此外,我注释掉了array = text.split(/n|r/g);,因为它在您的代码中没有做任何事情。无论如何,您都不希望在运行JSON.parse之前拆分JSON输入。

我希望这有助于澄清问题。

最新更新