* @ngdoc service * @kind function * @name $http * @requires ng.$httpBackend * @requires $cacheFactory * @requires $rootScope * @requires $q * @requires $injector * * @description * The `$http` service is a core Angular service that facilitates commun
(requestConfig)
| 8143 | </example> |
| 8144 | */ |
| 8145 | function $http(requestConfig) { |
| 8146 | var config = { |
| 8147 | method: 'get', |
| 8148 | transformRequest: defaults.transformRequest, |
| 8149 | transformResponse: defaults.transformResponse |
| 8150 | }; |
| 8151 | var headers = mergeHeaders(requestConfig); |
| 8152 | |
| 8153 | extend(config, requestConfig); |
| 8154 | config.headers = headers; |
| 8155 | config.method = uppercase(config.method); |
| 8156 | |
| 8157 | var serverRequest = function(config) { |
| 8158 | headers = config.headers; |
| 8159 | var reqData = transformData(config.data, headersGetter(headers), config.transformRequest); |
| 8160 | |
| 8161 | // strip content-type if data is undefined |
| 8162 | if (isUndefined(reqData)) { |
| 8163 | forEach(headers, function(value, header) { |
| 8164 | if (lowercase(header) === 'content-type') { |
| 8165 | delete headers[header]; |
| 8166 | } |
| 8167 | }); |
| 8168 | } |
| 8169 | |
| 8170 | if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) { |
| 8171 | config.withCredentials = defaults.withCredentials; |
| 8172 | } |
| 8173 | |
| 8174 | // send request |
| 8175 | return sendReq(config, reqData, headers).then(transformResponse, transformResponse); |
| 8176 | }; |
| 8177 | |
| 8178 | var chain = [serverRequest, undefined]; |
| 8179 | var promise = $q.when(config); |
| 8180 | |
| 8181 | // apply interceptors |
| 8182 | forEach(reversedInterceptors, function(interceptor) { |
| 8183 | if (interceptor.request || interceptor.requestError) { |
| 8184 | chain.unshift(interceptor.request, interceptor.requestError); |
| 8185 | } |
| 8186 | if (interceptor.response || interceptor.responseError) { |
| 8187 | chain.push(interceptor.response, interceptor.responseError); |
| 8188 | } |
| 8189 | }); |
| 8190 | |
| 8191 | while(chain.length) { |
| 8192 | var thenFn = chain.shift(); |
| 8193 | var rejectFn = chain.shift(); |
| 8194 | |
| 8195 | promise = promise.then(thenFn, rejectFn); |
| 8196 | } |
| 8197 | |
| 8198 | promise.success = function(fn) { |
| 8199 | promise.then(function(response) { |
| 8200 | fn(response.data, response.status, response.headers, config); |
| 8201 | }); |
| 8202 | return promise; |