【发布时间】:2012-07-08 14:19:59
【问题描述】:
我正在尝试使用 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
【问题讨论】:
-
您是否检查了来自 ajax 调用的响应?你有什么数据吗?
-
这可能是跨域请求,所以可能行不通。此外,您有“IE6、IE5 代码”注释这一事实也很糟糕。
-
@ftom2 :检查响应是什么意思?我已经根据功能改变了div的内容。还不够吗?
-
在 xmlhttp.responseText 上做一个 alert 或 console.log,我猜它是空的,然后问题出在服务器端没有发送数据。另外,如果可以的话,我建议你开始使用 jquery,它会让你的生活更轻松。
-
警报返回为空。这真的很奇怪,因为我在 vbscript 中有这个最简单的代码可以工作,但我无法让其他任何工作。 (我在 PHP 中需要它)。
标签: ajax xmlhttprequest