Created
August 26, 2023 07:32
-
-
Save edvinmolla/60f531c7e46cdaa4fe73facb769b16e2 to your computer and use it in GitHub Desktop.
Template post/get django
This file contains hidden or 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> | |
<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