This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathexercise-2.js
More file actions
54 lines (41 loc) · 1.52 KB
/
exercise-2.js
File metadata and controls
54 lines (41 loc) · 1.52 KB
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
/*
========
Task 2
========
Your second task is to send a new message to the server.
After writing a message in the input and clicking on the submit button,
send the message to store it on the remote server. Use the following API:
HTTP Verb: POST
API: https://codeyourfuture.herokuapp.com/api/messages
Request Body: { "content": "some text" }
===============
Expected result
===============
After opening index.html in your browser, write a message in the input field and click
on the submit button. Then check the following:
1) The input field should be empty.
2) When you refresh the page in your browser, you should be able to see your new message in the message list.
*/
// Write your code here
var postRequestParameters = {
body: JSON.stringify({ "content":new Date() }),
method: 'POST',
headers: {
'content-type': 'application/json'
}
};
fetch('https://codeyourfuture.herokuapp.com/api/messages',postRequestParameters);
fetch('https://codeyourfuture.herokuapp.com/api/messages')
.then(function(response){
return response.json();
})
.then(function(messages){
//document.querySelector('#message-list').innerHTML ="";
document.querySelector('#message-list').innerHTML+='<p>'+messages[messages.length-1].content +'</p>'
// messages.forEach(function(message){
// document.querySelector('#message-list').innerHTML+='<p>'+message.content +'</p>'
// // let newParagraph = document.createElement('p');
// // newParagraph.innerText
// // document.querySelector('message-list').appendChild(newParagraph);
// })
})