-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
37 lines (34 loc) · 1.26 KB
/
script.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
function fetchJSON() {
// parameters for the HTTP/S call
let params = {
mode: 'cors', // if you need to turn off CORS, use no-cors
headers: { // any HTTP headers you want can go here
'accept': 'application/json',
'connection': 'keep-alive'
}
}
// make the HTTP/HTTPS call:
fetch('https://dweet.io/get/dweets/for/my-thing-name')
.then(response => response.json()) // convert response to JSON
.then(data => getResponse(JSON.stringify(data))) // get the body of the response
.catch(error => getResponse(error));// if there is an error
}
function fetchText() {
// parameters for the HTTP/S call
let params = {
mode: 'cors', // if you need to turn off CORS, use no-cors
headers: { // any HTTP headers you want can go here
'accept': 'application/text',
'connection': 'keep-alive'
}
}
// make the HTTP/S call:
fetch('https://httpbin.org/encoding/utf8', params)
.then(response => response.text()) // convert response to text
.then(data => getResponse(data)) // get the body of the response
.catch(error => getResponse(error));// if there is an error
}
// function to call when you've got something to display:
function getResponse(data) {
document.getElementById('result').innerHTML = data;
}