猿问

我用ajax调用文本内容,但是在浏览其中就是没法显示,请大神们帮帮忙!!!谢谢了!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Ajax</title>

</head>


<body>

<div id="new" ></div>

<script>

function getHTTPObject(){

if(typeof XMLHttpRequest=="undefined")

 XMLHttpRequest=function(){

 try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

 catch(e){}

 try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

 catch(e){}

 try{return new ActiveXObjec("Msxml2.XMLHTTP");}

 catch(e){}

 try{return new ActiveXObject("Microsoft.XMLHTTP");}

 catch(e){}

 return false;

 }

 return new XMLHttpRequest();

}

function getNewContent(){

var request=getHTTPObject();

if(request){

request.open("GET","example.txt",true);

request.onreadystatechange=function(){

if(request.readyState==4){

var para= document.createElement("p");

var txt=document.createTextNode(request.responseText);

para.appendChild(txt);

document.getElementById('new').appendChild(para);

}

};

request.send(null);

}else{

alert('Sorry,your browser dont\'t support XMLhttpRequest');

}

alert("Function Done")

}


function addLoadEvent(func){

var onload=window.onload;

if(typeof (window.onload) !='function'){

window.onload=func;

}else{

window.onload=function(){

onload();

func();

}

}

}


addLoadEvent(getNewContent);

addLoadEvent(getHTTPObject);

</script>


</body>

</html>

文本和这个html在一个文件夹下,txt内容:你好!

闯货的红灯3128172
浏览 1428回答 3
3回答

Elem

换个浏览器。非http请求,有的浏览器不支持

杰杰就是我

我运行的结果为 弹出 Function Done , 然后页面出现 你好!  不知道你想表达的是什么错误 
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答