我是编码新手。我想刷新
'测试文本'在HTMLdiv id="结果"中到"或者其他什么"在data.php文件中
我不明白为什么它不起作用,因为我从视频课程中复制了那个代码。我正在Microsoft WebMatrix上运行这些页面。我尝试安装XAMPP,但无法启动Apache服务器。所以我最终选择了WebMatrix。感谢帮助新手
HTML文件
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function ajax_request() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var result = document.getElementById('result');
result.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('Get', 'data.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onclick="ajax_request">Click me</button>
<div id='result'>Test text</div>
</body>
</html>
PHP文件
<?php
$data = "Or something else";
echo $data;
?>
<button onclick="ajax_request">Click me</button>
必须是
<button onclick="ajax_request();">Click me</button>