Last active
January 9, 2018 09:42
-
-
Save rubinhozzz/0af4d54721d21fc47790624b6d8bc053 to your computer and use it in GitHub Desktop.
AJAX call example
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
function makeAjaxCall(url, methodType, callback){ | |
return $.ajax({ | |
url : url, | |
method : methodType, | |
dataType : "json" | |
}) | |
} | |
let data = serializeForm(document.getElementById('form_pv')); | |
makeAjaxCall(urlCreatePV, data, 'POST').then(function(respJson){ | |
if (respJson.ok == false) | |
return; | |
console.log(respJson); | |
$('#dialog_form_pv').modal('hide'); | |
// inserting into select2 | |
let pvName = $('#id_pv-cproduct').select2('data')[0].text + ' - ' + document.getElementById('id_pv-name').value; | |
let data = { | |
id: respJson.pk, | |
text: pvName | |
}; | |
let newOption = new Option(data.text, data.id, true, true); | |
$('#id_product_variant').append(newOption).trigger('change'); | |
}, function(reason){ | |
console.log("error in processing your request", reason); | |
errors = reason.responseJSON; | |
for (let field in errors){ | |
let element = document.getElementById('id_pv-' + field); | |
element.className += ' error-input'; | |
element.closest('.form-group').className += ' has-error'; | |
element.parentNode.querySelector('.error-block').innerHTML = errors[field][0]; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment