-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.html
42 lines (40 loc) · 1.44 KB
/
test.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
<!doctype html>
<input id="id" value="1" />
<input id="first" value="aaa" />
<input id="last" value="bbb" />
<button id="update">update</button>
<button id="create">create</button>
<button id="refresh">refresh</button>
<div id="list"></div>
<script>
//var url = "http://localhost:8000"
var url = "http://rem-rest-api.herokuapp.com"
var useCredentials = true
refresh.onclick = function() {
var x = new XMLHttpRequest()
x.open("GET", url + "/api/users?limit=50", true)
x.withCredentials = useCredentials
x.send()
x.onload = function() {
var data = JSON.parse(x.responseText)
list.innerHTML = data.data.map(d => "<li>" + d.id + " - " + d.firstName + " " + d.lastName + "</li>").join("")
}
}
refresh.onclick()
update.onclick = function() {
var x = new XMLHttpRequest()
x.open("PUT", url + "/api/users/" + id.value, true)
x.withCredentials = useCredentials
x.setRequestHeader("Content-Type", "application/json")
x.send(JSON.stringify({id: parseInt(id.value), firstName: first.value, lastName: last.value, val: 123}))
x.onload = function() {console.log(JSON.parse(x.responseText))}
}
create.onclick = function() {
var x = new XMLHttpRequest()
x.open("POST", url + "/api/users", true)
x.withCredentials = useCredentials
x.setRequestHeader("Content-Type", "application/json")
x.send(JSON.stringify({firstName: first.value, lastName: last.value, val: 234}))
x.onload = function() {console.log(JSON.parse(x.responseText))}
}
</script>