我部署在服务器上了,text的能够显示出来,但是 XML文件显示不出来
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My First Ajax Script</title>
<script src="script01.js"></script>
</head>
<body>
<p>
<a id="makeTextRequest" href="gAddress.txt">Request a text file</a><br>
<a id="makeXMLRequest" href="us-states.xml">Request an XML file</a>
</p>
<div id="updateArea"></div>
</body>
</html>
JS代码如下:
window.onload = initAll;
var xhr = false;
function initAll(){
document.getElementById("makeTextRequest").onclick = getNewFile;
document.getElementById("makeXMLRequest").onclick = getNewFile;
}
function getNewFile(){
makeRequest(this.href);
return false;
}
function makeRequest(url){
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}
else{
if(window.ActiveXObject){
try{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(xhr){
xhr.onreadystatechange = showContents;
xhr.open("GET",url,true);
xhr.send(null);
}
else{
document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}
function showContents(){
if(xhr.readyState == 4){
if(xhr.status == 200){
if(xhr.responseXML && xhr.responseXML.childNodes.length > 0){
var outMsg = getText(xhr.responseXML.getElementByTagName("choices")[0]);
}
else{
var outMsg = xhr.responseText;
}
}
else{
var outMsg = "There was a problem with the request" + xhr.status;
}
document.getElementById("updateArea").innerHTML = outMsg;
}
function getText(inVal){
if(inVal.textContent){
return inVal.textContent;
}
return inVal.text;
}
}