forked from csxiaoyaojianxian/JavaScriptStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-ajax get.html
More file actions
executable file
·39 lines (34 loc) · 1.07 KB
/
Copy path01-ajax get.html
File metadata and controls
executable file
·39 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>使用get发送数据到服务器</h1>
<input type="button" value="发送get_ajax请求" id='btnAjax'>
</body>
</html>
<script type="text/javascript">
// 绑定点击事件
document.querySelector('#btnAjax').onclick = function () {
// 发送ajax 请求 需要 五步
// 1.创建异步对象
var ajaxObj = new XMLHttpRequest();
// 2.设置请求的url等参数
// 参数1 请求的方法 参数2 请求的url
ajaxObj.open('get','senddata.php?userName=csxiaoyao');
// 3.发送请求
ajaxObj.send();
// 4.注册事件
// onreadystatechange 状态改变就会调用
ajaxObj.onreadystatechange = function () {
// 为了保证 数据 完整回来,一般会判断 两个值
if (ajaxObj.readyState==4&&ajaxObj.status==200) {
// 5.在注册的事件中 获取 返回的 内容 并修改页面的显示
// 数据是保存在 异步对象的 属性中
console.log(ajaxObj.responseText);
}
}
}
</script>