Skip to content

Instantly share code, notes, and snippets.

@oliverm2112
Last active April 25, 2017 09:02
Show Gist options
  • Save oliverm2112/5414702 to your computer and use it in GitHub Desktop.
Save oliverm2112/5414702 to your computer and use it in GitHub Desktop.
app.factory('httpService', ['$http', '$rootScope', function ($http, $rootScope) {
var service = {},
name;
const GATEWAY = 'http://localhost:3000/';
const CFGATEWAY = 'http://localhost:8501/angular/gateway.cfm';
$rootScope.$on('post.test', function (event, data) {
name = event.name;
service.post(data);
});
$rootScope.$on('ping', function (event, data) {
name = event.name;
service.head(data.url);
});
$rootScope.$on('crew.get', function (event, data) {
name = event.name;
console.log('on ' + name, 'event:', event, 'data:', data);
service.get(data.method, data.params, data.cache);
});
service.post = function (data) {
$http({
method : 'POST',
url : CFGATEWAY,
data : data
})
.success(function (data, status, headers, config) {
console.log('Post OK!!!', data);
$rootScope.$broadcast(name + '.success', data);
})
.error(function (data, status, headers, config) {
console.log('POST error', data);
$rootScope.$broadcast(name + '.error', 'data:', data, 'status:', status, 'headers:', headers);
});
};
service.get = function (method, params, cache) {
$http({
method : 'GET',
url : GATEWAY + method,
params : {param1 : 'hello'},
cache : cache ? true : false
}
)
.success(function (data, status, headers, config) {
$rootScope.$broadcast(name + '.success', data);
})
.error(function (data, status, headers, config) {
console.log('GET error', data);
$rootScope.$broadcast(name + '.error', data);
});
};
service.head = function (url) {
$http({
method : 'HEAD',
url : url,
params : {param1 : 'hello'},
cache : false
}
)
.success(function (data, status, headers, config) {
$rootScope.$broadcast(name + '.success', data);
})
.error(function (data, status, headers, config) {
console.log('HEAD error');
$rootScope.$broadcast(name + '.error', data);
}
);
};
}]);
@Zivpp
Copy link

Zivpp commented Apr 25, 2017

Thanks, It let me work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment