Javascript从文本文件加载数组
因此,为了快速解释我是否将Javascript从文本文件加载数组,javascript,html,arrays,json,Javascript,Html,Arrays,Json,因此,为了快速解释我是否将myObj从=array更改为myObj={callback.txt文件的内容} 这样就行了。但是,当我尝试将内容调用到var中时,当我将数组调用到innerHTML中时,我看不到页面上的完整内容。因此,我知道文件正在正确加载,但是我无法让它解析文件以获取我想要的信息,或者将其作为数组运行 将数组文件设置为 { "information": [ { "Telephone # Dialed": "855555
myObj从=array
更改为myObj={callback.txt文件的内容}
这样就行了。但是,当我尝试将内容调用到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数组=[];
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var text=xmlhttp.responseText;
数组=text.split(/\n |\r/g);
var obj=JSON.parse(文本);
数组=对象信息;
var listener=document.getElementById(“submitThis”);
listener.onclick=function(){
var事件=document.getElementById(“事件”).value;
var myObj,i,j,x=“”;
myObj=阵列;
对于(我在myObj.信息中){
if(myObj.information[i].IncidentID==事件){
对于(myObj.information[i].CallbackNumber中的j){
x=myObj.information[i].回拨号码;
}
}
}
document.getElementById(“test”).innerHTML=x;
}
}
}
open(“GET”、“callback.txt”、true);
xmlhttp.send()代码>
您的问题是试图访问数组中不存在的对象(您在代码前面已经调用了该对象)
var数组=[];
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var text=xmlhttp.responseText;
//数组=text.split(/\n |\r/g);
var obj=JSON.parse(文本);
数组=对象信息;
var listener=document.getElementById(“submitThis”);
listener.onclick=function(){
var事件=document.getElementById(“事件”).value;
var myObj,i,j,x=“”;
myObj=阵列;
对于(我在myObj){
if(myObj[i].IncidentID==事件){
对于(myObj[i]中的j).回拨号码){
x=myObj[i]。回拨号码;
}
}
}
document.getElementById(“test”).innerHTML=x;
}
}
}
open(“GET”、“callback.txt”、true);
xmlhttp.send();
Atarray=obj.information代码>您已经访问了信息。然后尝试在for循环中再次获取它,并在for循环中获取代码(即:for(i in myObj.information){
)
我还注释掉了array=text.split(/\n |\r/g)
,因为它在代码中没有做任何事情。而且在运行JSON.parse
之前,您也不想拆分JSON输入
我希望这有助于澄清问题。谢谢您的帮助。
<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>