2010-07-14 46 views
0
<html> 
<body> 
<SCRIPT type="text/javascript"> 
var xmlHttp = new XMLHttpRequest(); 
    var async = true; 

    xmlHttp.open("GET", "http://www.google.com", async); 
    if(async) 
    { 
     xmlHttp.onreadystatechange = function() 
     { 
      if(xmlHttp.readyState == 4) 
      { 
       if (xmlHttp.status==200) alert("It works!") 
       else if (xmlHttp.status==0) alert("Arggggg!") 
       else alert("Status is "+xmlHttp.status) 
      } 
     } 
    } 
    xmlHttp.send(); 
</script> 

</body> 
</html> 

我只是好奇XMLHttpRequest来查看它并正常工作,但我无法获得状态为非零。这些例子看起来很简单,但它不起作用。我已经尝试了大约4个例子。有没有搞错 ?XMLHttpRequest简单的HTTP GET不工作?

我想要做的就是读取网页并以纯文本格式查看HTML。

回答

3

我认为这是你的问题。

http://en.wikipedia.org/wiki/Same_origin_policy

简而言之,你无法通过一个XMLHttpRequest,因为页/ JS没有从谷歌服务的接入谷歌。

+0

就是这样。这是一个用你的代码返回一个404的例子:http://jsfiddle.net/dFyjt/ – Andir 2010-07-14 13:48:31