封装自定义服务$http

时间:2024-07-02 19:35:32
var httpService = angular.module('httpService', []);

httpService.factory("$httpService",function ($http, $q) {
var $httpService = {};
$httpService.getData = function (url,method,params) {
var defer = $q.defer(),
urls = "http://xxxx.com" + url,
headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'};
if (method === 'GET') {
$http({
url: urls,
method: "GET",
headers: headers,
params: params,
}).success(function (data) { defer.resolve(data);
}).
error(function (data, status, headers, config) {
defer.reject(data);
});
} else {
$http({
url: urls,
method: method,
headers: headers,
data: params,
}).success(function (data) {
defer.resolve(data);
}).
error(function (data, status, headers, config) {
// defer.resolve(data);
defer.reject(data);
});
}
return defer.promise;
};
return $httpService;
});