Skip to content

Instantly share code, notes, and snippets.

@ekinertac
Created May 4, 2015 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekinertac/edd241e35fbdb076574b to your computer and use it in GitHub Desktop.
Save ekinertac/edd241e35fbdb076574b to your computer and use it in GitHub Desktop.
deferred angular request
params.callback = 'JSON_CALLBACK';
// Create promise
var deffered = $q.defer();
// Do request
$http({
method: 'JSONP',
url: 'http://www.example.com/rest-api',
params: params,
cache: false
}).success(function(data) {
// Check results was found
if (data.status === 200 && data.results.length) {
// Resolve with success
deffered.resolve({
status: 200,
results: data.results
});
} else {
// Resolve with no content
deffered.resolve({
status: 204
});
}
// If error then simply resolve with error
}).error(function() {
// Resolve with error
deffered.resolve({
status: 500
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment