Skip to content

Instantly share code, notes, and snippets.

@edvinmolla
Created August 26, 2023 07:32
Show Gist options
  • Save edvinmolla/60f531c7e46cdaa4fe73facb769b16e2 to your computer and use it in GitHub Desktop.
Save edvinmolla/60f531c7e46cdaa4fe73facb769b16e2 to your computer and use it in GitHub Desktop.
Template post/get django
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<button id="get">Send GET Request</button><br>
<button id="post">Send POST Request</button>
<script>
var csrftoken = '{{ csrf_token }}'
document.getElementById('get').onclick = () => {
const requestObj = new XMLHttpRequest()
requestObj.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText)
}
}
requestObj.open("GET", '/get/')
requestObj.send()
}
document.getElementById('post').onclick = () => {
const requestObj = new XMLHttpRequest()
requestObj.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText)
}
}
requestObj.open("POST", '/post/')
requestObj.setRequestHeader("X-CSRFToken", csrftoken)
const formdata = new FormData()
formdata.append('name', 'John')
formdata.append('age', '17')
requestObj.send(formdata)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment