欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

封装自定义服务$http

程序员文章站 2022-05-14 07:53:38
var httpService = angular.module('httpService', []);httpService.factory("$httpService",function ($http, $q) { var $httpService = {}; $httpService.getD ......
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;
});