Skip to content

Instantly share code, notes, and snippets.

@kapv89
Last active December 18, 2015 17:19
Show Gist options
  • Save kapv89/5817677 to your computer and use it in GitHub Desktop.
Save kapv89/5817677 to your computer and use it in GitHub Desktop.
An ajax service free of all the angular http hassles (implements promises jquery style)
$provide.factory('$ajax', function ($rootScope) {
return function (options, callbacks, ctx) {
if(typeof(callbacks) === 'function') {
callbacks = { success: callbacks, error: function (data) { console.log(data) } };
}
options.success = function (data) {
ctx ? callbacks.success.call(ctx, data) : callbacks.success(data);
$rootScope.$digest();
};
options.error = function (res) {
console.log(res);
var data = JSON.parse(res.responseText);
//console.log(data, res);
ctx ? callbacks.error.call(ctx, data) : callbacks.error(data);
if(res.status === 403 || res.status === 401) {
$rootScope.$broadcast('auth-failed');
}
};
options.data = options.data || {};
options.type = options.type || 'GET';
options.dataType = options.dataType || 'json';
return jQuery.ajax(options);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment