Created
June 8, 2020 12:34
-
-
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"
This file contains 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
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