-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4. JSON.js
More file actions
36 lines (25 loc) · 687 Bytes
/
4. JSON.js
File metadata and controls
36 lines (25 loc) · 687 Bytes
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
// JSON Objects example
// [ {"text":"hey, "num":1}, {"text":"hi, "num":2}, {"text":"hola, "num":3} ]
const Todos = (callback)=>{
const request = new XMLHttpRequest();
request.addEventListener('readystatechange',
()=>{
if(request.readyState===4 && request.status===200){
const data = JSON.parse(request.responseText) //JSON objects to JS array
callback(undefined,data)
}else if(request.readyState===4){
callback('could not fetch data',undefined)
}
}
)
request.open('GET','https://jsonplaceholder.typicode.com/todos/');
request.send();
}
Todos((err,data)=>{
console.log('callback fired');
if(err){
console.log(err)
}else{
console.log(data)
}
})