Last active
November 25, 2019 08:46
-
-
Save tobykurien/94c2058cff78dbeca46c4191ea6cf5ea to your computer and use it in GitHub Desktop.
Browser function example
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
<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