Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本地加载普通,但在发布时加载不正确_Javascript_Json_Html_Audio - Fatal编程技术网

Javascript 本地加载普通,但在发布时加载不正确

Javascript 本地加载普通,但在发布时加载不正确,javascript,json,html,audio,Javascript,Json,Html,Audio,我正在尝试创建一个在线工作的小型RadioPlayer。这是我的JavaScript代码 function changeLink(link) { /* this changes the link and plays the radio*/ var radio= document.getElementById("radio"); radio.src = link; radio.play(); } function jsonLoad() { var xmlhttp = new XMLHttp

我正在尝试创建一个在线工作的小型RadioPlayer。这是我的JavaScript代码

function changeLink(link)
{
/* this changes the link and plays the radio*/
var radio= document.getElementById("radio");
radio.src = link;
radio.play();

}

function jsonLoad()
{
  var xmlhttp = new XMLHttpRequest();
  var url = "playList.txt";

  xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          var myArr = JSON.parse(xmlhttp.responseText);
          readJSON(myArr);
      }
  }
  xmlhttp.open("GET", url, true);
  xmlhttp.send();

   }

function readJSON(obj)
{
    var list = "<ui>";
  for (var i = 0; i < obj.length ; i++) 
  {
      list += "<li><a href='javascript:changeLink(" +"\"" + obj[i].radioLink + "\"" +");'>" + obj[i].name + "</a></li>";
     }
  list += "</ui>";

  document.getElementById("radioLoad").innerHTML = list;
}
功能更改链接(链接)
{
/*这将更改链接并播放收音机*/
var radio=document.getElementById(“radio”);
radio.src=链接;
收音机。播放();
}
函数jsonLoad()
{
var xmlhttp=new XMLHttpRequest();
var url=“playList.txt”;
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var myArr=JSON.parse(xmlhttp.responseText);
readJSON(myArr);
}
}
open(“GET”,url,true);
xmlhttp.send();
}
函数readJSON(obj)
{
var list=“”;
对于(变量i=0;i”;
}
列表+=“”;
document.getElementById(“radioLoad”).innerHTML=list;
}
这也是HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title> My Radio Player</title>
    <script src="Script.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body onload="jsonLoad()">

    <p>This is the link that i will change his property</p>
    <audio id="radio" src="#" controls autoplay preload="auto" ></audio>


    </audio>
    <br>
    <p id="radioLoad"> 

    </p> 




</body>

我的收音机
这是我将改变他的财产的链接


无论如何,谢谢:)

如果不亲自调试,我不能确定,但我很确定,当您尝试获取对象的
.length
属性(JSON.parse()返回javascript对象)时,您的问题出在
readJSON()
函数中,因为对象没有
.length
属性。要查找对象的键数,可以使用
object.keys(myArr).length
。因此,在for循环中,
obj.length
为Null,因此
i
不能小于Null,因此它不会执行。因此,如果将
i
替换为
i
,应该可以解决问题。

如果有帮助的话,可以作为旁注。我正在使用WebMatrix创建我的网站并发布。现在我在本地没有这个问题,而这发生在服务器上?我无法编辑它:我尝试了你的想法,在本地工作,但仍然无法在服务器上工作。我还注意到,这些代码可能只在google chrome中工作。Firefox 3.5 Internet Explorer 8 chrome Opera 10 Safari 4这些是支持JSON.parse()的web浏览器。仍然无法解释它在本地工作,但不能在服务器上工作。我会尽力给你一个答案。