Anew_G 2012年05月04日
用XMLHTTPRequest获取网页源代码的疑问?

function addLoadEvent(func){
var oldonload=window.onload;
if(typeof window.onload!='function'){
window.onload=func;
}else{
window.onload=function(){
oldonload();
func();
}
}
}


addLoadEvent(doth);


var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

var req=null;
var console=null;


function sendRequest(url,params,HttpMethod){
if(!HttpMethod){
HttpMethod="GET";
}
req=initXMLHTTPRequest();
if(req){
req.onreadystatechange=onReadyStateChange;
req.open(HttpMethod,url,true);
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
req.setRequestHeader("Pragma", "no-cache");

req.send(params);
}
}

function initXMLHTTPRequest(){
var xRequest=null;
if(window.XMLHttpRequest){
xRequest=new XMLHttpRequest();
}else if(typeof ActiveXObject!="undefined"){
xRequest=new ActiveXObject
("Microsoft.XMLHTTP");
}
return xRequest;
}

function onReadyStateChange(){
var ready=req.readyState;
check=req.readyState;
var data=" ";
if(ready==READY_STATE_COMPLETE){
data=req.responseText;


}
toConsole(data);
}

function toConsole(data){
if(console!=null){
var newline=document.createElement("div");
newline.id="comedata";
console.appendChild(newline);
var txt=document.createTextNode(data);
newline.appendChild(txt);


}
}

function doth(){
console=document.getElementById('console');
sendRequest("data.txt")
}




获取本地data.txt里的内容一切正常。
我将data.txt换成url为什么显示的结果是一片空白呢?
...全文
108 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
JavaScript
创建于2007-09-28

5.1w+

社区成员

22.3w+

社区内容

Web 开发 JavaScript
社区公告
暂无公告