feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Parse headers into key value object
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} headers Raw headers as a string
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @returns {Object} Parsed headers as key value object
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
function parseHeaders(headers) {
|
|
|
|
|
|
var parsed = {}, key, val, i;
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
if (!headers) return parsed;
|
|
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
forEach(headers.split('\n'), function(line) {
|
|
|
|
|
|
i = line.indexOf(':');
|
|
|
|
|
|
key = lowercase(trim(line.substr(0, i)));
|
|
|
|
|
|
val = trim(line.substr(i + 1));
|
|
|
|
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
|
|
if (parsed[key]) {
|
|
|
|
|
|
parsed[key] += ', ' + val;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
parsed[key] = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns a function that provides access to parsed headers.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Headers are lazy parsed when first requested.
|
|
|
|
|
|
* @see parseHeaders
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {(string|Object)} headers Headers to provide access to.
|
|
|
|
|
|
* @returns {function(string=)} Returns a getter function which if called with:
|
|
|
|
|
|
*
|
|
|
|
|
|
* - if called with single an argument returns a single header value or null
|
|
|
|
|
|
* - if called with no arguments returns an object containing all headers.
|
|
|
|
|
|
*/
|
2012-01-04 17:21:05 +00:00
|
|
|
|
function headersGetter(headers) {
|
|
|
|
|
|
var headersObj = isObject(headers) ? headers : undefined;
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
return function(name) {
|
2012-01-04 17:21:05 +00:00
|
|
|
|
if (!headersObj) headersObj = parseHeaders(headers);
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
if (name) {
|
2012-01-04 17:21:05 +00:00
|
|
|
|
return headersObj[lowercase(name)] || null;
|
2011-12-28 17:26:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-04 17:21:05 +00:00
|
|
|
|
return headersObj;
|
2011-12-28 17:26:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Chain all given functions
|
|
|
|
|
|
*
|
|
|
|
|
|
* This function is used for both request and response transforming
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {*} data Data to transform.
|
2012-01-04 17:21:05 +00:00
|
|
|
|
* @param {function(string=)} headers Http headers getter fn.
|
|
|
|
|
|
* @param {(function|Array.<function>)} fns Function or an array of functions.
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
* @returns {*} Transformed data.
|
|
|
|
|
|
*/
|
2012-01-04 17:21:05 +00:00
|
|
|
|
function transformData(data, headers, fns) {
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
if (isFunction(fns))
|
2012-01-04 17:21:05 +00:00
|
|
|
|
return fns(data, headers);
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
forEach(fns, function(fn) {
|
2012-01-04 17:21:05 +00:00
|
|
|
|
data = fn(data, headers);
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
function isSuccess(status) {
|
|
|
|
|
|
return 200 <= status && status < 300;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
function $HttpProvider() {
|
2011-11-05 01:02:47 +00:00
|
|
|
|
var JSON_START = /^\s*(\[|\{[^\{])/,
|
2011-11-05 00:15:03 +00:00
|
|
|
|
JSON_END = /[\}\]]\s*$/,
|
|
|
|
|
|
PROTECTION_PREFIX = /^\)\]\}',?\n/;
|
|
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
var $config = this.defaults = {
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// transform incoming response data
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
transformResponse: function(data) {
|
|
|
|
|
|
if (isString(data)) {
|
2011-10-19 00:03:48 +00:00
|
|
|
|
// strip json vulnerability protection prefix
|
2011-11-05 00:15:03 +00:00
|
|
|
|
data = data.replace(PROTECTION_PREFIX, '');
|
|
|
|
|
|
if (JSON_START.test(data) && JSON_END.test(data))
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
data = fromJson(data, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// transform outgoing request data
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
transformRequest: function(d) {
|
|
|
|
|
|
return isObject(d) ? toJson(d) : d;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// default headers
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
common: {
|
|
|
|
|
|
'Accept': 'application/json, text/plain, */*',
|
|
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
|
|
},
|
|
|
|
|
|
post: {'Content-Type': 'application/json'},
|
|
|
|
|
|
put: {'Content-Type': 'application/json'}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
var providerResponseInterceptors = this.responseInterceptors = [];
|
2011-11-30 08:58:34 +00:00
|
|
|
|
|
2012-01-07 03:17:31 +00:00
|
|
|
|
this.$get = ['$httpBackend', '$browser', '$cacheFactory', '$rootScope', '$q', '$injector',
|
|
|
|
|
|
function($httpBackend, $browser, $cacheFactory, $rootScope, $q, $injector) {
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
var defaultCache = $cacheFactory('$http'),
|
|
|
|
|
|
responseInterceptors = [];
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
forEach(providerResponseInterceptors, function(interceptor) {
|
|
|
|
|
|
responseInterceptors.push(isString(interceptor) ? $injector.get(interceptor) : interceptor);
|
|
|
|
|
|
});
|
2011-12-01 23:54:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc function
|
|
|
|
|
|
* @name angular.module.ng.$http
|
|
|
|
|
|
* @requires $httpBacked
|
|
|
|
|
|
* @requires $browser
|
|
|
|
|
|
* @requires $cacheFactory
|
2012-01-07 03:17:31 +00:00
|
|
|
|
* @requires $rootScope
|
|
|
|
|
|
* @requires $q
|
|
|
|
|
|
* @requires $injector
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {object} config Object describing the request to be made and how it should be processed.
|
|
|
|
|
|
* The object has following properties:
|
|
|
|
|
|
*
|
|
|
|
|
|
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
|
|
|
|
|
|
* - **url** – `{string}` – Absolute or relative URL of the resource that is being requested.
|
|
|
|
|
|
* - **data** – `{string|Object}` – Data to be sent as the request message data.
|
|
|
|
|
|
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
|
|
|
|
|
|
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
|
|
|
|
|
|
* GET request, otherwise if a cache instance built with $cacheFactory, this cache will be
|
|
|
|
|
|
* used for caching.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns {HttpPromise} Returns a promise object with the standard `then` method and two http
|
|
|
|
|
|
* specific methods: `success` and `error`. The `then` method takes two arguments a success and
|
|
|
|
|
|
* an error callback which will be called with a response object. The `success` and `error`
|
|
|
|
|
|
* methods take a single argument - a function that will be called when the request succeeds or
|
|
|
|
|
|
* fails respectively. The arguments passed into these functions are destructured representation
|
|
|
|
|
|
* of the response object passed into the `then` method. The response object has these
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
*
|
|
|
|
|
|
* - **data** – `{string|Object}` – The response body transformed with the transform functions.
|
|
|
|
|
|
* - **status** – `{number}` – HTTP status code of the response.
|
|
|
|
|
|
* - **headers** – `{function([headerName])}` – Header getter function.
|
|
|
|
|
|
* - **config** – `{Object}` – The configuration object that was used to generate the request.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @property {Array.<Object>} pendingRequests Array of config objects for pending requests.
|
|
|
|
|
|
* This is primarily meant to be used for debugging purposes.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* $http is a service through which XHR and JSONP requests can be made.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function $http(config) {
|
|
|
|
|
|
config.method = uppercase(config.method);
|
2011-11-30 08:58:34 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
var reqTransformFn = config.transformRequest || $config.transformRequest,
|
|
|
|
|
|
respTransformFn = config.transformResponse || $config.transformResponse,
|
|
|
|
|
|
defHeaders = $config.headers,
|
|
|
|
|
|
reqHeaders = extend({'X-XSRF-TOKEN': $browser.cookies()['XSRF-TOKEN']},
|
|
|
|
|
|
defHeaders.common, defHeaders[lowercase(config.method)], config.headers),
|
2012-01-04 17:21:05 +00:00
|
|
|
|
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
|
2011-12-28 17:26:22 +00:00
|
|
|
|
promise;
|
2011-11-24 11:53:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// send request
|
|
|
|
|
|
promise = sendReq(config, reqData, reqHeaders);
|
|
|
|
|
|
|
2011-11-24 11:53:04 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// transform future response
|
|
|
|
|
|
promise = promise.then(transformResponse, transformResponse);
|
|
|
|
|
|
|
|
|
|
|
|
// apply interceptors
|
|
|
|
|
|
forEach(responseInterceptors, function(interceptor) {
|
|
|
|
|
|
promise = interceptor(promise);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
promise.success = function(fn) {
|
|
|
|
|
|
promise.then(function(response) {
|
|
|
|
|
|
fn(response.data, response.status, response.headers, config);
|
|
|
|
|
|
});
|
|
|
|
|
|
return promise;
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
};
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
promise.error = function(fn) {
|
|
|
|
|
|
promise.then(null, function(response) {
|
|
|
|
|
|
fn(response.data, response.status, response.headers, config);
|
|
|
|
|
|
});
|
|
|
|
|
|
return promise;
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
};
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
|
|
|
|
function transformResponse(response) {
|
|
|
|
|
|
// make a copy since the response must be cacheable
|
|
|
|
|
|
var resp = extend({}, response, {
|
2012-01-04 17:21:05 +00:00
|
|
|
|
data: transformData(response.data, response.headers, respTransformFn)
|
2011-12-28 17:26:22 +00:00
|
|
|
|
});
|
|
|
|
|
|
return (isSuccess(response.status))
|
|
|
|
|
|
? resp
|
|
|
|
|
|
: $q.reject(resp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$http.pendingRequests = [];
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#get
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `GET` request
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {HttpPromise} Future object
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#delete
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `DELETE` request
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {HttpPromise} Future object
|
|
|
|
|
|
*/
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#head
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `HEAD` request
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {XhrFuture} Future object
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#patch
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `PATCH` request
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {HttpPromise} Future object
|
|
|
|
|
|
*/
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#jsonp
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `JSONP` request
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request.
|
|
|
|
|
|
* Should contain `JSON_CALLBACK` string.
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {XhrFuture} Future object
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*/
|
2012-01-10 18:17:05 +00:00
|
|
|
|
createShortMethods('get', 'delete', 'head', 'jsonp');
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#post
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `POST` request
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {*} data Request content
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {HttpPromise} Future object
|
|
|
|
|
|
*/
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
|
* @name angular.module.ng.$http#put
|
|
|
|
|
|
* @methodOf angular.module.ng.$http
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Shortcut method to perform `PUT` request
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {*} data Request content
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
|
|
|
|
|
* @returns {XhrFuture} Future object
|
|
|
|
|
|
*/
|
|
|
|
|
|
createShortMethodsWithData('post', 'put');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $http;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createShortMethods(names) {
|
|
|
|
|
|
forEach(arguments, function(name) {
|
|
|
|
|
|
$http[name] = function(url, config) {
|
|
|
|
|
|
return $http(extend(config || {}, {
|
|
|
|
|
|
method: name,
|
|
|
|
|
|
url: url
|
|
|
|
|
|
}));
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
function createShortMethodsWithData(name) {
|
|
|
|
|
|
forEach(arguments, function(name) {
|
|
|
|
|
|
$http[name] = function(url, data, config) {
|
|
|
|
|
|
return $http(extend(config || {}, {
|
|
|
|
|
|
method: name,
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
data: data
|
|
|
|
|
|
}));
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
/**
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* Makes the request
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* !!! ACCESSES CLOSURE VARS:
|
|
|
|
|
|
* $httpBackend, $config, $log, $rootScope, defaultCache, $http.pendingRequests
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
*/
|
2011-12-28 17:26:22 +00:00
|
|
|
|
function sendReq(config, reqData, reqHeaders) {
|
|
|
|
|
|
var deferred = $q.defer(),
|
|
|
|
|
|
promise = deferred.promise,
|
|
|
|
|
|
cache,
|
|
|
|
|
|
cachedResp;
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
$http.pendingRequests.push(config);
|
|
|
|
|
|
promise.then(removePendingReq, removePendingReq);
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
if (config.cache && config.method == 'GET') {
|
|
|
|
|
|
cache = isObject(config.cache) ? config.cache : defaultCache;
|
|
|
|
|
|
}
|
2011-11-04 23:34:47 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
if (cache) {
|
|
|
|
|
|
cachedResp = cache.get(config.url);
|
|
|
|
|
|
if (cachedResp) {
|
|
|
|
|
|
if (cachedResp.then) {
|
2012-01-07 03:17:31 +00:00
|
|
|
|
// cached request has already been sent, but there is no response yet
|
2011-12-28 17:26:22 +00:00
|
|
|
|
cachedResp.then(removePendingReq, removePendingReq);
|
|
|
|
|
|
return cachedResp;
|
2011-11-04 23:34:47 +00:00
|
|
|
|
} else {
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// serving from cache
|
|
|
|
|
|
if (isArray(cachedResp)) {
|
|
|
|
|
|
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resolvePromise(cachedResp, 200, {});
|
|
|
|
|
|
}
|
2011-11-04 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// put the promise for the non-transformed response into cache as a placeholder
|
|
|
|
|
|
cache.put(config.url, promise);
|
2011-11-04 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// if we won't have the response in cache, send the request to the backend
|
|
|
|
|
|
if (!cachedResp) {
|
|
|
|
|
|
$httpBackend(config.method, config.url, reqData, done, reqHeaders, config.timeout);
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
return promise;
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-10-31 18:34:28 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback registered to $httpBackend():
|
|
|
|
|
|
* - caches the response if desired
|
|
|
|
|
|
* - resolves the raw $http promise
|
|
|
|
|
|
* - calls $apply
|
|
|
|
|
|
*/
|
|
|
|
|
|
function done(status, response, headersString) {
|
|
|
|
|
|
if (cache) {
|
|
|
|
|
|
if (isSuccess(status)) {
|
|
|
|
|
|
cache.put(config.url, [status, response, parseHeaders(headersString)]);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// remove promise from the cache
|
|
|
|
|
|
cache.remove(config.url);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolvePromise(response, status, headersString);
|
|
|
|
|
|
$rootScope.$apply();
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Resolves the raw $http promise.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function resolvePromise(response, status, headers) {
|
|
|
|
|
|
// normalize internal statuses to 0
|
|
|
|
|
|
status = Math.max(status, 0);
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
(isSuccess(status) ? deferred.resolve : deferred.reject)({
|
|
|
|
|
|
data: response,
|
|
|
|
|
|
status: status,
|
|
|
|
|
|
headers: headersGetter(headers),
|
|
|
|
|
|
config: config
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2011-10-18 05:52:21 +00:00
|
|
|
|
|
feat($http): new $http service, removing $xhr.*
Features:
- aborting requests
- more flexible callbacks (per status code)
- custom request headers (per request)
- access to response headers
- custom transform functions (both request, response)
- caching
- shortcut methods (get, head, post, put, delete, patch, jsonp)
- exposing pendingCount()
- setting timeout
Breaks Renaming $xhr to $http
Breaks Takes one parameter now - configuration object
Breaks $xhr.cache removed - use configuration cache: true instead
Breaks $xhr.error, $xhr.bulk removed
Breaks Callback functions get parameters: response, status, headers
Closes #38
Closes #80
Closes #180
Closes #299
Closes #342
Closes #395
Closes #413
Closes #414
Closes #507
2011-08-04 23:24:41 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
function removePendingReq() {
|
|
|
|
|
|
var idx = indexOf($http.pendingRequests, config);
|
|
|
|
|
|
if (idx !== -1) $http.pendingRequests.splice(idx, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}];
|
|
|
|
|
|
}
|