以下代码不起作用,我不知道为什么。即服务器文件中的文本未在屏幕上打印。因此,我要更改的文本不会更改。
设置的第一部分ajaxRequest
适用于其他示例代码。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//Browser Support Code
function ajaxFunction()
{
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
我很确定问题出在下面的某个地方,但我看不出在哪里:
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{
document.getElementById("time").innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST", "serverTime.php", true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
<p id="time">Original text</p>
</form>
</body>
</html>
以下是代码serverTime.php
:
<?php
echo "Hello world.";
?>
当我在“用户名”输入中键入“test”时,浏览器看起来像这样:
http://localhost/ajax_testing/ajax_tut.html?username=test