Php 使用AJAX读取XML文件时出错
当我试图使用AJAX显示xml文件时,我的.html文件中出现了一个错误。我的程序应该有一个可以读取json文件的按钮和另一个可以读取xml文件的按钮。json按钮工作正常,但当我按下xml按钮时,它会给我一个错误,我无法找出它 我所犯的错误Php 使用AJAX读取XML文件时出错,php,jquery,ajax,xml,json,Php,Jquery,Ajax,Xml,Json,当我试图使用AJAX显示xml文件时,我的.html文件中出现了一个错误。我的程序应该有一个可以读取json文件的按钮和另一个可以读取xml文件的按钮。json按钮工作正常,但当我按下xml按钮时,它会给我一个错误,我无法找出它 我所犯的错误 Uncaught TypeError: Cannot set property 'innerHTML' of null index.html:34 这是我的密码: index.html <!DOCTYPE html> <html&g
Uncaught TypeError: Cannot set property 'innerHTML' of null index.html:34
这是我的密码:
index.html
<!DOCTYPE html>
<html>
<head>
<title>AJAX - responseJSON</title>
<script src="ajax.js"></script>
<script>
function getMovies() {
var xmlHttp = xmlHttpObjCreate();
if (!xmlHttp) {
alert("The browser doesn't support this action.");
return;
}
xmlHttp.onload = function() {
if (xmlHttp.status == 200) {
// Get XML Document
var xmlDoc = xmlHttp.responseXML;
// Variable for our output
var output = '';
// Build output by parsing XML
movieTitles = xmlDoc.getElementsByTagName('title');
for (i = 0; i < movieTitles.length; i++) {
output += movieTitles[i].childNodes[0].nodeValue + "<br>";
}
// Get div object
var divObj = document.getElementById('movieBox');
// Set the div's innerHTML
divObj.innerHTML = output;
}
}
xmlHttp.open("GET", "movies.xml", true);
xmlHttp.overrideMimeType("text/xml")
xmlHttp.send();
}
function getContent() {
var xmlHttp = xmlHttpObjCreate();
if (!xmlHttp) {
alert("The browser doesn't support this action.");
return;
}
xmlHttp.onload = function() {
if (xmlHttp.status == 200) {
// Get Response Text
var response = xmlHttp.responseText;
// Prints the JSON string
console.dir(response);
// Get div object
var divObj = document.getElementById('contentBox');
// We used JSON.parse to turn the JSON string into an object
var responseObject = JSON.parse(response);
// This is our object
console.dir(responseObject);
// We can use that object like so:
divObj.innerHTML = "Hi I am " + responseObject.name + " and my pet is " + responseObject.pet;
}
}
xmlHttp.open("GET", "json.php", true);
xmlHttp.send();
}
</script>
</head>
<body>
<h3>My Content</h3>
<div id="contentBox"></div>
<button type="button" onclick="getContent();">Get Content</button>
<h3>My movies</h3>
<div id"movieBox"></div>
<button type="button" onclick="getMovies();"> Get Movies</button>
</body>
</html>
movies.xml
<?xml version= "1.0"?>
<collection>
<movies>
<title> Gone with the Wind
<year> 1939 </year>
</title>
<title> 2001: A Space Odyssey
<year> 1968 </year>
</title>
<title> Jurassic Park
<year> 1993 </year>
</title>
<title> The Shawshank Redmption
<year> 1994 </year>
</title>
<title> Balto
<year> 1995 </year>
</title>
</movies>
</collection>
飘
1939
2001年:太空漫游
1968
侏罗纪公园
1993
肖申克的救赎
1994
巴尔托
1995
无效的HTML:
<div id"movieBox"></div>
^---missing =
^---失踪=
没有=
,没有有效的id
属性,因此您的getElementById()
失败并返回null
表示失败
<div id"movieBox"></div>
^---missing =