【发布时间】:2020-07-24 20:26:16
【问题描述】:
所以快速解释我是否将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>
【问题讨论】:
标签: javascript html arrays json