/
jQuery-ajax.html
56 lines (53 loc) · 1.19 KB
/
jQuery-ajax.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
<!DOCTYPE html>
<html lang="zh-CN">
<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>cors</title>
<style>
body {
text-align: center;
}
ul {
list-style: none;
}
li {
border: 1px solid #0f0;
padding: 10px;
margin-bottom: 20px;
cursor: pointer;
}
li:hover {
background-color: #0f0;
}
</style>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<ul id="ct">
</ul>
<button id="load-news">加载新闻</button>
<script>
// 与普通的ajax唯一的区别是cors需要在服务器端设置响应头
var $btn = $('#load-news')
var $ct = $('#ct')
$btn.on('click', function() {
$.ajax({
type: 'get',
url: 'http://localhost:8080/cors',
dataType: 'json'
}).done(function(data) {
renderPage(data)
})
})
function renderPage(data) {
var html = $ct.html()
for (var key in data) {
html += '<li>' + data[key] + '</li>'
}
$ct.html(html)
}
</script>
</body>
</html>