-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
143 lines (138 loc) · 4.78 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!doctype html>
<html lang="it">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link lang="css" rel="icon" href="img/favicon.ico" type="image/gif" sizes="16x16">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style type="text/css">
.jumbotron {
position: fixed;
left: 50%;
top: 10%;
transform: translate(-50%, -10%);
width: 99%;
height: auto;
}
</style>
<title>FETCH | ivanpier</title>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Studio Fetch</h1>
<hr />
<p><button class="btn btn-outline-success">Bootstrao 4.0</button></p>
<div class="form-data">
<form id="form_data">
<div class="form-group">
<input type="text" id="title" placeholder="Title" class="form-control" autofocus="true" />
</div>
<textarea class="input-control" id="body" placeholder="Body"></textarea>
<div class="form-group">
<input type="submit" id="send_data" class="form-control btn btn-outline-primary" value="Send data">
</div>
</form>
</div>
</div>
</div> <!-- container -->
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- library google jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script lang="javascript">
// Public
document.querySelector('.form-data').addEventListener('submit', loaddataremote)
async function getDataSemplice() {
'use strict'
try {
const request = new Request('json.json', {
method: 'GET',
headers: {'Content-Type' : 'application/json'}
})
await fetch(request)
.then(rs => {
if(rs.status == 200) {
return rs.json()
}
}).then(db => {
console.table(db)
console.log(db.usr)
}).catch(err => {
console.error("Non trovo la pagina o errore di sintassi.")
})
} catch(Exception) {
console.error("Errore di sintassi.");
}
}
// getDataSemplice();
async function getDataArray() {
'use strict'
try {
const request = new Request('json_ar.json', {
method: 'GET',
headers: {'Content-Type' : 'application/json'}
//body: JSON.stringify(db)
});
await fetch(request)
.then(rs => {
if(rs.status == 200) {
return rs.json();
}
}).then(db => {
console.log(db.length)
console.table(db)
console.error("FOR EACH")
db.forEach(function(val, indice){
console.log(val) // stampa l'object
// ora cerchiamo di visulaizzare il dato pulisto come stringa
console.log(`${val.age} \n`)
})
}).catch(er => {console.error("Non trovo la pagina o sintassi non corretta.")})
} catch(Exception){
console.error(Exception.message);
}
}
// getDataArray();
async function loaddataremote(event) {
'use strict'
try {
event.preventDefault();
let title = document.querySelector('#title').value;
let body = document.querySelector('#body').value;
const db = {
title : title,
body : body
}
await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {'Content-Type' : 'application/json'},
body: JSON.stringify(db)
})
.then((res) => {
// console.log(res)
// if (res.status == 201) {
if(res.ok) {
return Promise.resolve(res.json());
} else {
return Promise.reject({status: res.status, statusText: res.statusText});
}
})
.then((data) => console.table(data))
.catch(err => console.error('Error message: ', err.statusText));
} catch(Exception) {
console.error(Exception.message);
}
}
</script>
<!--
In questo tutorial vediamo come installare il Live Server di Visual Studio Code.
Eseguire il server (raccomandazione la index, deve avere estensione .html).
-->
</body>
</html