Skip to content

Instantly share code, notes, and snippets.

@tobykurien
Last active November 25, 2019 08:46
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 tobykurien/94c2058cff78dbeca46c4191ea6cf5ea to your computer and use it in GitHub Desktop.
Save tobykurien/94c2058cff78dbeca46c4191ea6cf5ea to your computer and use it in GitHub Desktop.
Browser function example
<div id="success">
<b>Success!</b>
<p>
Thank you for submitting your message. We will get back to you ASAP.
</p>
</div>
<div id="failed">
<b>Failed!</b>
<p>
Something went wrong, sorry. Try again later.
</p>
</div>
<script>
// The form POST data comes through as the args parameter
async function main(args, md) {
let env = md.env; // environment variables from environment.json
let res = await addDocument(env.db_url, env.db_key, env.db_token, args);
if (res.ok) {
return document.getElementById("success").innerHTML;
} else {
return document.getElementById("failed").innerHTML;
}
}
// Generic function to Add a document to CouchDB. Returns a Promise.
function addDocument(dbUrl, dbKey, dbToken, data) {
return fetch(dbUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(dbKey + ":" + dbToken)
},
body: JSON.stringify(data)
}).then(function (response) {
return response.json();
})
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment