Skip to content

Commit fa7a6c8

Browse files
samples for AJAX GET and POST requests with Fetch API
1 parent 930a735 commit fa7a6c8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Web-Fetch-API-Get/index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Web Fetch API for a GET request</title>
7+
</head>
8+
<body>
9+
<div id="books"></div>
10+
<script>
11+
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
12+
13+
fetch('http://localhost/PHP-Dump/src/ajaxcalldump.php?key1=value1&key2=value2',{
14+
method: 'GET'
15+
}).then((reponse) => {
16+
// console.log(reponse);
17+
if (reponse.ok) {
18+
reponse.json().then((json) => {
19+
console.log(json);
20+
})
21+
}
22+
});
23+
</script>
24+
</body>
25+
</html>

Web-Fetch-API-Post/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Web Fetch API for a POST request</title>
7+
</head>
8+
<body>
9+
<div id="books"></div>
10+
<script>
11+
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
12+
13+
fetch('http://localhost/PHP-Dump/src/ajaxcalldump.php',{
14+
method: 'POST',
15+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
16+
body: 'key1=value1&key2=value2'
17+
}).then((reponse) => {
18+
// console.log(reponse);
19+
if (reponse.ok) {
20+
reponse.json().then((json) => {
21+
console.log(json);
22+
})
23+
}
24+
});
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)