-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMC4212-2c.html
48 lines (44 loc) · 1.17 KB
/
MC4212-2c.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
43
44
45
46
47
48
//USING FETCH() TO POST JSON DATA:
<html>
<head>
</head>
<body>
<form id="form" method="post">
<input type="text", id="name" placeholder="Name"/></br>
<input type="text", id="body" placeholder="Body"/></br>
<input type="submit" value="Add"/>
</form>
<div>
<h3>The Following data is successfuly posted</h3>
<h4 id="title"></h4>
<h5 id="bd"></h5>
</div>
</body>
<script>
var form=document.getElementById('form')
form.addEventListener('submit', function(e){
e.preventDefault()
var name=document.getElementById('name').value
var body=document.getElementById('body').value
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title:name,
body:body,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
}
})
.then(function(response){
return response.json()})
.then(function(data)
{console.log(data)
title=document.getElementById("title")
body=document.getElementById("bd")
title.innerHTML = data.title
body.innerHTML = data.body
}).catch(error => console.error('Error:', error));
});
</script>
</html>