-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr.html
101 lines (90 loc) · 3.4 KB
/
xhr.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>XMLHttpRequest</title>
<link rel="shortcut icon" href="#" type="image/x-icon">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<style>
.card-group{
width:80vw;
margin:auto;
}
.card-group .card{
flex:unset;
padding:1rem;
border:0;
}
.card-group .card .card-body{
flex:unset;
padding:0.5rem;
border:1px solid #ccc;
border-radius: 0.5rem;
min-height:15rem;
}
.name{
font-weight: bolder;
border-bottom:1px solid #999;
margin:0.25rem 0;
}
.summary{
border-bottom:1px solid #ddd;
box-shadow: 0 0 5px #ccc;
margin:0.5rem 0;
width:90%;
float:right;
}
</style>
</head>
<body>
<h1 class="header">XMLHttpRequest</h1>
<div class="title"></div>
<h4></h4>
<ul class="card-group">
</ul>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-VHvPCCyXqtD5DqJeNxl2dtTyhF78xXNXdkwX1CZeRusQfRKp+tA7hAShOK/B/fQ2" crossorigin="anonymous"></script>
</body>
</html>
<script>
/********************************************************
* 1.api網址 https://kktix.com/events.json
* 2.宣告一個XMLHttpRequest物件
* 3.建立請求的各項設定及實質內容
* 4.建立得到回應時的各項行為
* 5.送出請求
********************************************************/
//宣告一個XMLHttpRequest物件
let xhr=new XMLHttpRequest;
//建立請求的各項設定及實質內容
xhr.open('get','https://kktix.com/events.json')
//建立得到回應時的各項設定及行為
xhr.onload=function(){
let type=xhr.getResponseHeader("Content-Type")
let status=xhr.status
let res=xhr.responseText
let data=JSON.parse(res)
let title=document.querySelector(".title")
title.innerHTML=data.title
//console.log(data)
let ul=document.querySelector('ul')
let li='';
let entrys=data.entry
for(let i=0;i<entrys.length;i++){
li=li+`<li class='card col-3'>
<div class='card-body'>
${entrys[i].title}
<div class="name">${entrys[i].author.name}</div>
<div class="summary">${entrys[i].summary}</div>
</div>
</li>`;
}
ul.innerHTML=li
}
//送出請求
xhr.send();
</script>