根据对使用Ajax的html页面的建议,我决定尝试学习它是如何工作的。在我的例子中,我只是试图从一个PHP文件(它只是回应一个简单的字符串作为测试)得到响应,但它不起作用,因为没有发生任何事情。基本ajax逻辑
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Incident Center </title>
<!--<link rel="stylesheet" href="http://web.njit.edu/~swp5/assignment/style/style.css">-->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function onsubmit()
{
var sender;
if (window.XMLHttpRequest)
{
sender=new XMLHttpRequest();
}
else
{
sender=new ActiveXObject("Microsoft.XMLHTTP");
}
sender.onreadystatechange=function()
{
if (sender.readyState==4 && sender.status==200)
{
document.getElementById("myDiv").innerHTML=sender.responseText;
}
}
sender.open("GET","proz.php",true);
sender.send();
}
</script>
</head>
<body>
<div class="header">
Incident Center
</div>
<p>
<button onclick="onsubmit()">Test</button>
</p>
<div id="myDiv"></div>
</body>
</html>
比如:你的HTML格式不正确。缺少'
对不起,为了简化代码,我将代码从原始状态改为了一点,以便缩小可能性。我忘了删除另一个标签。 – Sam
您是否在请求后检查了响应代码? responseText仅在sender.status == 200时设置。 –