Skip to content

Instantly share code, notes, and snippets.

@walison17
Last active July 14, 2020 23:48
Show Gist options
  • Save walison17/b6bb4eb43c1b8581ee4b2403367d0641 to your computer and use it in GitHub Desktop.
Save walison17/b6bb4eb43c1b8581ee4b2403367d0641 to your computer and use it in GitHub Desktop.
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