我们首先要确保XML文件是否正常执行
如果访问时候报类似 Parse error: syntax error, unexpected 'version' (T_STRING 这样的错误,我们则需要修改php的配置文件:
将这里改成Off然后从其apache即可。。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function createXHR(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;
} function test1(){
var xhr = createXHR();
xhr.open('GET','7.php',true);
xhr.onreadystatechange = function(){
if(this.readyState == 4){
//针对xml,ajax也给出了一种特殊的方法进行接收responseXML
//alert(this.responseXML);
var xmldom = this.responseXML;
var chs = xmldom.getElementsByTagName('name')[0];
//alert(chs.firstChild.firstChild.wholeText);
//alert(chs.lastChild.firstChild.wholeText);
document.getElementById('mpname').innerHTML = chs.firstChild.firstChild.wholeText;
document.getElementById('mpdesc').innerHTML = chs.lastChild.firstChild.wholeText;
}
}
xhr.send(null);
}
</script>
</head>
<body>
<div>
<h1>获奖名单</h1>
<p>歌名:<span id="mpname"></span></p>
<p>简介:<span id="mpdesc"></span></p>
</div>
<input type="button" value="显示获奖歌曲" onclick="test1();">
<input type="button" value="text2" onclick="test2();">
</body>
</html>
<?php
header("Content-Type:text/xml");
?>
<?xml version='1.0' encoding='UTF-8'?>
<mp3><name bid="a666"><title>忘情水</title><desc>忘了就好</desc></name></mp3>