Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created December 23, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/f27bb30f9d548eaadf4896f39fe2fb31 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/f27bb30f9d548eaadf4896f39fe2fb31 to your computer and use it in GitHub Desktop.
simple fetch post demo
<!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