Last active
July 14, 2020 23:48
-
-
Save walison17/b6bb4eb43c1b8581ee4b2403367d0641 to your computer and use it in GitHub Desktop.
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
const serialize = function (data) { | |
const formData = new FormData(); | |
for ([key, value] of Object.entries(data)) { | |
Array.isArray(value) | |
? value.forEach(v => formData.append(key, v)) | |
: formData.append(key, value); | |
} | |
return formData; | |
} | |
# ... | |
const csrfToken = document.querySelector('input[name="csrfmiddlewaretoken"]').value; | |
fetch("/pacientes", { | |
method: 'POST', | |
body: serialize(data), | |
headers: { | |
'X-CSRFToken': csrfToken | |
} | |
}) | |
.then(res => res.json()) | |
.then(json => { | |
json.ok ? window.location.href = json.redirect_url | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment