diff --git a/jams-server/src/main/resources/webapp/js/api.js b/jams-server/src/main/resources/webapp/js/api.js index 0567f9f4f98bd4f1026b679bb6f4b55ce4f22c5d..67ee67040ca78723ddb2cf8f49fd61d71f2e9a4e 100644 --- a/jams-server/src/main/resources/webapp/js/api.js +++ b/jams-server/src/main/resources/webapp/js/api.js @@ -58,71 +58,77 @@ var api_path_get_user_search = '/api/admin/users'; function ajaxApiCall(api_path, request_type, data, credentials, callBackFunction, async) { // build AJAX call - var ajax = { - url: url_path + ":" + url_port + api_path, - type: request_type, - crossDomain: true, - dataType: "json", - success: function(data, statusCode, jqXHR) { - callBackFunction(data, statusCode, jqXHR); - }, - error: function(data, statusCode, jqXHR) { - callBackFunction(data, statusCode, jqXHR); - }}; - - // pass credentials in the header - if (credentials) { - ajax['headers'] = { - "Authorization": "Basic " + btoa(credentials["username"] + ':' + credentials["password"]), - } - } - if (window.localStorage.getItem('access_token')) { + return new Promise(function(resolve) { + + var ajax = { + url: url_path + ":" + url_port + api_path, + type: request_type, + crossDomain: true, + dataType: "json", + success: function (data, statusCode, jqXHR) { + callBackFunction(data, statusCode, jqXHR); + }, + error: function (data, statusCode, jqXHR) { + callBackFunction(data, statusCode, jqXHR); + } + }; - var jwt = localStorage.getItem('access_token'); - ajax['headers'] = { - "Authorization": "Bearer " + jwt, + // pass credentials in the header + if (credentials) { + ajax['headers'] = { + "Authorization": "Basic " + btoa(credentials["username"] + ':' + credentials["password"]), + } } - } - // pass data in the header - if (data) { - if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search || - (api_path == api_path_post_create_user && request_type == 'POST') || api_path == api_path_post_update_user - || api_path == api_path_get_auth_devices || api_path == api_path_post_configuration_change_password) - isSearch = true; + if (window.localStorage.getItem('access_token')) { - // search dataType - if (isSearch) { - ajax['data'] = data; + var jwt = localStorage.getItem('access_token'); + ajax['headers'] = { + "Authorization": "Bearer " + jwt, + } } - else { - if (window.localStorage.getItem('access_token')) { - var jwt = localStorage.getItem('access_token'); + // pass data in the header + if (data) { + if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search || api_path == api_path_get_user_search || + (api_path == api_path_post_create_user && request_type == 'POST') || api_path == api_path_post_update_user + || api_path == api_path_get_auth_devices || api_path == api_path_post_configuration_change_password) + isSearch = true; + + // search dataType + if (isSearch) { + ajax['data'] = data; + } + else { + + if (window.localStorage.getItem('access_token')) { + var jwt = localStorage.getItem('access_token'); - ajax['headers'] = { - "Authorization": "Bearer " + jwt, - } - } else { - ajax['headers'] = { - "Content-type":"application/json" + ajax['headers'] = { + "Authorization": "Bearer " + jwt, + } + } else { + ajax['headers'] = { + "Content-type": "application/json" + } } + + ajax['data'] = JSON.stringify(data); } + } - ajax['data'] = JSON.stringify(data); + // async + if (!async) { + ajax['async'] = async; } - } - // async - if (!async) { - ajax['async'] = async; - } + $.ajax(ajax); + isSearch = false; + resolve(data); + }); - $.ajax(ajax); - isSearch = false; - return data; } function setJWT(value) {