Skip to content

Instantly share code, notes, and snippets.

@lucaslgr
Created June 8, 2020 12:34
Show Gist options
  • Save lucaslgr/6d0bbcb631a33d4e6fd372f25bb0f58d to your computer and use it in GitHub Desktop.
Save lucaslgr/6d0bbcb631a33d4e6fd372f25bb0f58d to your computer and use it in GitHub Desktop.
Getting the response of ajax request but the flow doesn't enter in ".then"
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('form-edit input[type=submit]').addEventListener('click', (event) => {
event.preventDefault();
let formData = new FormData(event.target.closest('form'));
const url = '<?php echo BASE_URL ?>home/edit/<?= $_SESSION[md5(BASE_URL) . '_' . 'cLogin'] ?>';
const params = {
method: 'POST',
body: formData,
dataType: 'application/json'
};
fetch(url, params)
.then((response) => {
response.json()
})
.then((resJSON) => {
if (resJSON.status == true) {
alert('Seus dados foram alterados com sucesso!')
window.location.href = "<?= BASE_URL ?>home/index";
} else {
alert('ERRO! Seus dados não foram alterados!')
window.location.href = "<?= BASE_URL ?>home/edit/<?= $_SESSION[md5(BASE_URL) . '_' . 'cLogin'] ?>";
}
});
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment