forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-jsonp.js
40 lines (32 loc) · 929 Bytes
/
app-jsonp.js
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
// requires jQuery to use JSONP
// https://learn.jquery.com/ajax/working-with-jsonp/
/* global document, jQuery */
/* eslint-disable no-console */
function updateFavoriteFruits (contents) {
if (typeof contents !== 'string') {
contents = contents.map((item) => `<li>${item}</li>`).join('')
}
document.querySelector('.favorite-fruits').innerHTML = contents
}
function getFavoriteFruits () {
const favFruits = document.querySelector('.favorite-fruits')
if (!favFruits) {
return
}
favFruits.innerHTML = '<div class="loader"></div>'
jQuery.ajax({
url: '/favorite-fruits-jsonp',
jsonp: 'fruitsCallback',
dataType: 'jsonp',
success (fruits) {
updateFavoriteFruits(fruits)
},
error (req, message, err) {
console.error(message)
console.error('error for request', req)
console.error(err)
},
})
}
// get the list of fruits on startup
getFavoriteFruits()