封装自定义服务$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;
});
上一篇: vue实现添加标签demo示例代码
下一篇: 输入、输出流