0

我正在尝试使用 AJAX 获取页面的内容,但没有得到任何结果..

这是代码:

<script type="text/javascript">
function onSumResponse() {
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.arihav.com/",true);
xmlhttp.send();
}

</script>

这是正文中的 div:

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="onSumResponse()">Change Content</button>

点击后我得到的是空白 div。

编辑:此代码取自w3schools

编辑 2:这是 vbscript 中有效的代码:

GotothisURL = "http://www.arihav.com"
Set GetConnection = CreateObject("Microsoft.XMLHTTP")
GetConnection.Open "get", GotothisURL, False
GetConnection.Send
ResponsePage = GetConnection.responseText
4

1 回答 1

0

如果你想请求另一个主机更好地使用 curl 或 JSON ,AJAX 不允许访问不同的服务器由于源策略

于 2012-07-08T14:52:17.710 回答