Last active
December 18, 2015 17:19
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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