Created
December 23, 2018 11:40
-
-
Save cosminpopescu14/f27bb30f9d548eaadf4896f39fe2fb31 to your computer and use it in GitHub Desktop.
simple fetch post demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<script> | |
let clicks = 0; | |
const url = `fetch.php`; | |
function clickMe() { | |
clicks += 1; | |
document.getElementById("clicks").innerHTML = clicks; | |
sendClicks(url, {nr_clicks: clicks, ip: "192.168.0.45"}) | |
} | |
function sendClicks(url = ``, data = {}){ | |
return fetch(url, { | |
method: "post", | |
//mode: "cors", | |
//cache: "no-cache", | |
//credentials: "same-origin", | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
//redirect: "follow", | |
//referrer: "no-referrer", | |
body: JSON.stringify(data), | |
}) | |
.then(response => response.json()) | |
.then(data => console.log(data)) | |
.catch(err => console.error(err)); | |
} | |
</script> | |
<a href="https://stackoverflow.com/questions/22402777/html-javascript-button-click-counter" onclick="clickMe()" target="_blank">Click</a> | |
<p>Clicks: <a id="clicks">0</a></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment