Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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 XMLHttpRequest获取JSON文件的内容,但返回[object]_Javascript_Json - Fatal编程技术网

Javascript XMLHttpRequest获取JSON文件的内容,但返回[object]

Javascript XMLHttpRequest获取JSON文件的内容,但返回[object],javascript,json,Javascript,Json,我遵循w3schools的指南,试图更好地理解JSON 这是他们的密码 这是他们的示例JSON文件 使用XMLHttpRequest获取文件的内容。 内容是以JSON格式编写的,可以轻松地转换为JavaScript对象 var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var myObj=JSON.parse(this.r

我遵循w3schools的指南,试图更好地理解JSON

这是他们的密码

这是他们的示例JSON文件


使用XMLHttpRequest获取文件的内容。
内容是以JSON格式编写的,可以轻松地转换为JavaScript对象

var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var myObj=JSON.parse(this.responseText); document.getElementById(“demo”).innerHTML=myObj.name; } }; open(“GET”,“json_demo.txt”,true); xmlhttp.send(); 看看

这里有另一个我想使用的JSON文件示例

使用相同的代码,除了更改

document.getElementById(“demo”).innerHTML=myObj.name

document.getElementById(“demo”).innerHTML=myObj

它似乎没有带回来任何东西,除了[对象],我不明白为什么,有人能帮忙吗,谢谢

  <!DOCTYPE html>
<html>
<body>



<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj;
  }
};
xmlhttp.open("GET", "https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json", true);
xmlhttp.send();
</script>
</body>
</html>

var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var myObj=JSON.parse(this.responseText); document.getElementById(“demo”).innerHTML=myObj; } }; open(“GET”https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json“,对); xmlhttp.send();
如果要将收到的JSON对象打印到DOM中,需要将其转换为字符串。要做到这一点,请使用
JSON.stringify(responseText)

,或者首先跳过对响应的解析
JSON.parse()
生成一个响应。试图产生
[object object]
您实际上想要实现什么?您想在

元素中打印什么?
  <!DOCTYPE html>
<html>
<body>



<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj;
  }
};
xmlhttp.open("GET", "https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json", true);
xmlhttp.send();
</script>
</body>
</html>