-
Notifications
You must be signed in to change notification settings - Fork 35
/
ajax.jsonp.html
119 lines (115 loc) · 3.38 KB
/
ajax.jsonp.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Пример использования JSONP</title>
<link rel="profile" href="https://gmpg.org/xfn/11"/>
<link rel="shortcut icon" href="https://anton.shevchuk.name/favicon.ico"/>
<link rel="stylesheet" href="css/styles.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/code.js"></script>
<script>
function toJsonContainer (data, status, xhr) {
console.log(data)
$('<code></code>')
.append(JSON.stringify(data))
.append("\n\n")
.appendTo('#json')
}
function buildImages (data) {
const images = document.getElementById('images')
$.each(data.items, function (i, item) {
let img = document.createElement('img')
img.alt = `Flickr image #${i}`
img.src = item.media.m
let div = document.createElement('div')
div.append(img)
images.append(div)
if (i === 3) return false
})
images.append(
document.createElement('hr')
)
}
</script>
<style>
#json code {
margin: 1rem;
padding: 1rem;
}
#images div {
height: 216px;
width: 310px;
float: left;
overflow: hidden;
margin: 1rem 2rem;
border: 1px solid #ccc;
border-radius: 4px;
}
#images div img {
min-height: 216px;
min-width: 310px;
border: 0;
padding: 0;
margin: 0;
}
hr {
clear: both;
}
</style>
</head>
<body>
<header>
<h1>Пример использования JSONP с простым сервером</h1>
<h2>
Код сервера <a href="https://anton.shevchuk.name/book/code/ajax/example.code">code/ajax/example.code</a>
</h2>
</header>
<main id="content">
<article>
<div id="json"></div>
</article>
<article>
<div id="images"></div>
</article>
</main>
<footer>
© <a href="https://anton.shevchuk.name/jquery-book/">jQuery for beginners</a>
</footer>
<aside>
<nav>
<a href="ajax.script.html" title="go prev" rel="prev">Prev</a>
<a href="index.html#60" title="back to Index" rel="index">Index</a>
<a href="#" title="reload" onclick="window.location.reload();return false">Reload</a>
<a href="ajax.transport.html" title="go next" rel="next">Next</a>
</nav>
<hr/>
<div>
<code>$.ajax({
url: <span>"ajax/example.php?callback=?"</span>,
dataType: <span>"jsonp"</span>,
success: toJsonContainer
});</code>
<button type="button">Run Code</button>
<code>$.ajax({
url: <span>"ajax/example.php?callback=?"</span>,
dataType: <span>"jsonp"</span>,
jsonpCallback: <span>"toJsonContainer"</span>
});</code>
<button type="button">Run Code</button>
<h3>Flickr API</h3>
<code contenteditable="true">$.getJSON(
<span>"https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?"</span>,
{
tags: <span>"orange"</span>,
tagmode: <span>"any"</span>,
format: <span>"json"</span>
},
buildImages
);</code>
<button type="button">Run Code</button>
</div>
</aside>
</body>
</html>