Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created March 29, 2019 14:51
Show Gist options
  • Select an option

  • Save SebDeclercq/221a331ef7e8538fb3ff01a61a3fcf19 to your computer and use it in GitHub Desktop.

Select an option

Save SebDeclercq/221a331ef7e8538fb3ff01a61a3fcf19 to your computer and use it in GitHub Desktop.
document.querySelector('form.save').addEventListener('submit', () => {
let data = FormData();
data.append('substitute', this.querySelector('input[name=substitute]'));
data.append('substituted', this.querySelector('input[name=substituted]'));
let xhr = new XmlHttpRequest();
xhr.open('POST', '{% url "favorite:save" %}');
xhr.addEventListener('load', () => {
const answer = JSON.parse(xhr.responseText);
if (xhr.status == 200) {
let status = answer.status;
if (status == 'success') {
alert('Favori sauvegardé !');
}
else {
alert('Une erreur est survenue');
}
}
else {
alert('Une erreur est survenue');
}
});
xhr.send(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment