<!--PHP编程实战-->
<!--JSON & Ajax -->
<!--15-6-->
<!--使用XMLHttpRequest抓取XML文件的内容并显示为 纯文本-->
<html>
<head>
<style type="text/css">
#generated_content {
border: 1px solid black;
widht: 300px;
background-color: #dddddd;
}
</style>
</head>
<body>
<p><strong>Ajax grabbed plain text:</strong></p>
<div id="generated_content"> </div>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "animals.xml", true);
xhr.onreadystatechange = function () {
//console.log(xhr.readyState);
if (xhr.readyState == 4) {
var message = "";
//console.log(xhr.status);
if (xhr.status = 200) {
// 将结果作为纯文本返回
console.log(xhr.responseText);
message = "<pre>" + xhr.responseText + "</pre>";
}
else {
message = "An error has occured making the request";
}
//xml文件内容并没有原样显示,而是剔除了标签
document.getElementById("generated_content").innerHTML = message;
}
}
xhr.send(null);
</script>
</body>
</html>
GetElementById
Element单数,结果唯一