-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxhr.html
More file actions
54 lines (45 loc) · 1.42 KB
/
xhr.html
File metadata and controls
54 lines (45 loc) · 1.42 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>XHR</title>
</head>
<body>
<h2></h2>
<h3></h3>
<script>
function request(url, method, cb) {
var xhr = new XMLHttpRequest()
xhr.open(method, url)
xhr.send()
xhr.onload = function () {
data = JSON.parse(xhr.responseText)
cb(null, data)
}
xhr.onerror = function () {
cb('Error')
}
}
// var url1 = 'https://jsonplaceholder.typicode.com/users/1'
// var url1 =
// 'https://api.digikala.com/v1/categories/mobile-phone/brands/apple/search/?camCode%5B0%5D=582&camCode%5B1%5D=582&sort=1&seo_url=%2Fcategory-mobile-phone%2Fapple%2F%3FcamCode%255B0%255D%3D582%26camCode%255B1%255D%3D582%26sort%3D1&page=1'
var url1 = 'http://localhost:3000/'
request(url1, 'GET', function (err, data) {
if (err) {
console.log('Error')
} else {
document.querySelector('h2').textContent = data.name
}
})
// var url2 = 'https://jsonplaceholder.typicode.com/users/2'
// request(url2, 'GET', function (err, data) {
// if (err) {
// alert('Error')
// } else {
// document.querySelector('h3').textContent = data.name
// }
// })
</script>
</body>
</html>