angular.module('AjaxService', []).service('Ajax', function ($http) { this.get = function (c, m, p, success, fail) { var url = '?class=' + c + '&method=' + m; if (typeof p === 'function') { fail = success; success = p; } else if (Array.isArray(p)) { url += '¶ms=' + p.map(encodeURIComponent).join(); } return $http({ timeout: 120000, url: url, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }).then(function (response) { if (success) { success(response.data); } }, function (response) { if (fail) { fail(response.status, response.statusText); } else { if (response.status == 401) { location.reload(); } } }); }; this.post = function (c, m, p, data, success, fail) { var url = '?class=' + c + '&method=' + m; if (typeof p === 'function') { fail = data; success = p; data = {}; } else if (typeof data === 'function') { fail = success; success = data; data = {}; if (Array.isArray(p)) { url += '¶ms=' + p.map(encodeURIComponent).join(); } else if (p) { data = p; } } else if (Array.isArray(p)) { url += '¶ms=' + p.map(encodeURIComponent).join(); } else if (p) { fail = success; success = data; data = p; } return $http({ timeout: 120000, method: "POST", url: url, data: $.param({"params": angular.toJson(data)}), headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function (response) { if (success) { success(response.data); } }, function (response) { if (fail) { fail(response.status, response.statusText); } else { if (response.status == 401) { location.reload(); } } }); }; });