
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>AJAX入门示例</title>
</head>
<body>
<button id="btn">确定</button>
<div id="result"></div>
<script>
// 绑定点击事件处理
document.getElementById('btn').onclick = function () {
// 创建Ajax对象实例
var xhr = new XMLHttpRequest();
// 创建Ajax对象并初始化,发送GET请求
xhr.open('GET', 'get_data.php', true);
// 当Ajax状态码变化时自动触发的回调函数
xhr.onreadystatechange = function () {
// 当Ajax状态码为4时,表示数据接收已完成
if (xhr.readyState === 4 && xhr.status === 200) {
// 提取返回结果
document.getElementById('result').innerHTML = xhr.responseText;
}
};
// 发起异步请求
xhr.send();
};
</script>
</body>
</html>
<?php
// 编写PHP代码用于返回数据
echo "Hello,这是来自服务器的响应数据!";
?>

