const url = '/'; function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function sendRequest(api, method, param, callback, errorCallback) { let _url = url; let path; const token = getCookie('token'); const beforeSend = function (xhr) { if (token) { xhr.setRequestHeader('Authorization', `${token}`); } } const success = function (response) { callback(response.data); } const error = function (jqXHR, exception) { var msg = ''; if (jqXHR.status === 0) { msg = 'Not connect.\n Verify Network.'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500].'; } else if (jqXHR.status == 401 || jqXHR.status == 403) { msg = 'Forbidden [' + jqXHR.status + '].'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = ''; try { let obj; //check for string json let str = jqXHR.responseText; if (str.indexOf('"{\\') === 0) { str = str.replace(/\\/g, ""); if (str && str.charAt(str.length - 1) === ',') { str = str.substring(1, str.length - 2); } else { str = str.substring(1, str.length - 1); } obj = JSON.parse(str); } else { obj = JSON.parse(JSON.stringify(jqXHR.responseJSON)); } //check for error message if (obj['message']) { msg = obj['message']; } else { for (let key in obj) { if (typeof obj[key] === 'string') { msg += obj[key] + '\n'; } else if (Array.isArray(obj[key])) { msg += obj[key].toString() + '\n'; } } } } catch (error) { console.log(error); msg = jqXHR.responseText; } } errorCallback(msg); } const opt = { type: method, url: _url + 'api/' + api, contentType: 'application/json; charset=utf-8', dataType: 'json', data: param ? JSON.stringify(param) : '', beforeSend, success, error, } if (param instanceof FormData) { opt['cache'] = false; opt['contentType'] = false; opt['processData'] = false; opt['data'] = param; if (method === 'PUT') { opt['type'] = 'POST'; param.append('_method', 'PUT'); } } else { opt['data'] = param ? JSON.stringify(param) : ''; } $.ajax(opt); } function getData(api, param, callback, errorCallback) { sendRequest(api, 'GET', param, callback, errorCallback); } function postData(api, param, callback, errorCallback) { sendRequest(api, 'POST', param, callback, errorCallback); } function putData(api, param, callback, errorCallback) { sendRequest(api, 'PUT', param, callback, errorCallback); } function delData(api, param, callback, errorCallback) { sendRequest(api, 'DELETE', param, callback, errorCallback); } function loadPicture(api, callback, errorCallback) { let _url = url; let path; const token = localStorage.getItem('token'); const beforeSend = function (xhr) { if (token) { xhr.setRequestHeader('Authorization', `${token}`); } } const success = function (response) { callback(response.data); } $.ajax({ url: _url + 'api/' + api, cache: false, xhr: function () {// Seems like the only way to get access to the xhr object var xhr = new XMLHttpRequest(); xhr.responseType = 'blob' return xhr; }, success: function (data) { var url = window.URL || window.webkitURL; callback(url.createObjectURL(data)); }, error: function () { errorCallback(); } }); }