37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
(function collonadeClosure() {
|
|
'use strict';
|
|
|
|
window.utils = window.utils || {};
|
|
|
|
window.utils.asyncForm = function(formElement, options) {
|
|
|
|
function setup() {
|
|
formElement.addEventListener('submit', function(event) {
|
|
event.preventDefault();
|
|
|
|
var url = formElement.getAttribute('action');
|
|
var headers = { };
|
|
var body = new FormData(formElement);
|
|
|
|
if (options && options.headers) {
|
|
Object.keys(options.headers).forEach(function(headerKey) {
|
|
headers[headerKey] = options.headers[headerKey];
|
|
});
|
|
}
|
|
|
|
window.utils.httpClient.post(url, headers, body)
|
|
.then(function(response) {
|
|
return response.json();
|
|
}).then(function(response) {
|
|
console.log('asyncForm: got response', response);
|
|
// TODO: process json response once backend returns json
|
|
}).catch(function(error) {
|
|
console.error('could not fetch or process response from ' + url, { error });
|
|
});
|
|
});
|
|
}
|
|
|
|
setup();
|
|
};
|
|
})();
|