-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax2.html
85 lines (64 loc) · 1.89 KB
/
ajax2.html
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX 2</title>
</head>
<body>
<button id="button1">Get User</button>
<button id="button2">Get Users</button>
<br>
<br>
<br>
<h1>User</h1>
<div id="user"></div>
<br>
<h1>Users</h1>
<div id="users"></div>
<script>
document.getElementById('button1').addEventListener('click', loadUser)
document.getElementById('button2').addEventListener('click', loadUsers)
const usersDiv = document.getElementById('users')
//Tekil kullanıcı örneği
function loadUser(){
const xhr = new XMLHttpRequest()
xhr.open('GET', 'user.json', true)
xhr.onload = function(){
if(this.status == 200) {
const user = JSON.parse(this.responseText)
let output = ''
output += '<ul>' +
'<li>' + user.id + '</li>' +
'<li>' + user.name + '</li>' +
'<li>' + user.email + '</li>' +
'</ul>'
document.getElementById('user').innerHTML= output
}
}
xhr.send()
}
//Çoğul kullanıcı örneği
function loadUsers(){
const xhr = new XMLHttpRequest()
xhr.open('GET', 'users.json', true)
xhr.onload = function(){
if(this.status == 200) {
const users = JSON.parse(this.responseText)
console.log(users)
let output = ''
users.forEach(function(user,index){
output += '<div>' + user.id + '</div>' +
'<div>' + user.name + '</div>' +
'<div>' + user.email + '</div>'
});
console.log(output)
usersDiv.innerHTML = output
}
}
xhr.send()
}
</script>
</body>
</html>