function hasToken() { return false;} function request(with_token,type,url,data,success_fct,cancel_fct,decompose_error) { if(decompose_error === undefined) { decompose_error = true; } if(with_token && !hasToken()) { if(cancel_fct) { cancel_fct([]); } } else { var arg = { type: type, url: '/api/' + url, headers: {'Content-Type': 'application/json'}, success: function (json) { if(success_fct) { success_fct(json); } }, error: function (jqXHR) { if(cancel_fct) { if(decompose_error) { let response_array = []; if(jqXHR.responseJSON) { for (var key in jqXHR.responseJSON) { if (jqXHR.responseJSON.hasOwnProperty(key)) { if(Array.isArray(jqXHR.responseJSON[key])) { jqXHR.responseJSON[key].forEach(function(msg) { response_array.push(msg); }); } else { response_array.push(jqXHR.responseJSON[key]); } } } } cancel_fct(response_array,jqXHR.status); } else { cancel_fct(jqXHR.responseJSON,jqXHR.status); } } } } if(data) { arg['data'] = JSON.stringify(data); } if(with_token) { arg['headers']['Authorization'] = 'Token '; } $.ajax(arg); } } function fetch_request(type,url,data,success_fct,cancel_fct) { if(!hasToken()) { if(cancel_fct) { cancel_fct([]); } } else { var canceled = false; fetch('/api/' + url, { method: type, headers: {'Authorization':'Token '}, body: data }).then(response => { if(response.ok) { return response.json(); } else { canceled = true; if(cancel_fct) { cancel_fct([],response.status); } return; } }).then(data => { if(success_fct && !canceled) { success_fct(data); } }).catch((error) => { console.error('Error:', error); if(cancel_fct && !canceled) { cancel_fct([]); } }); } }