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*$/,
|
2013-05-11 16:23:33 +00:00
|
|
|
|
PROTECTION_PREFIX = /^\)\]\}',?\n/,
|
|
|
|
|
|
CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'};
|
2011-11-05 00:15:03 +00:00
|
|
|
|
|
2012-07-30 00:52:19 +00:00
|
|
|
|
var defaults = this.defaults = {
|
2011-12-28 17:26:22 +00:00
|
|
|
|
// transform incoming response data
|
2012-03-27 15:53:44 +00:00
|
|
|
|
transformResponse: [function(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
|
|
|
|
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))
|
2013-08-27 22:57:01 +00:00
|
|
|
|
data = fromJson(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
|
|
|
|
}
|
|
|
|
|
|
return data;
|
2012-03-27 15:53:44 +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
|
|
|
|
// transform outgoing request data
|
2012-03-27 15:53:44 +00:00
|
|
|
|
transformRequest: [function(d) {
|
2012-02-26 02:49:54 +00:00
|
|
|
|
return isObject(d) && !isFile(d) ? toJson(d) : d;
|
2012-03-27 15:53:44 +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
|
|
|
|
|
|
|
|
|
|
// default headers
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
common: {
|
2012-10-12 19:41:05 +00:00
|
|
|
|
'Accept': 'application/json, text/plain, */*'
|
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
|
|
|
|
},
|
2013-05-11 16:23:33 +00:00
|
|
|
|
post: CONTENT_TYPE_APPLICATION_JSON,
|
|
|
|
|
|
put: CONTENT_TYPE_APPLICATION_JSON,
|
|
|
|
|
|
patch: CONTENT_TYPE_APPLICATION_JSON
|
2013-02-05 12:37:36 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
xsrfCookieName: 'XSRF-TOKEN',
|
|
|
|
|
|
xsrfHeaderName: 'X-XSRF-TOKEN'
|
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
|
|
|
|
};
|
|
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
/**
|
2013-09-28 03:53:36 +00:00
|
|
|
|
* Are ordered by request, i.e. they are applied in the same order as the
|
|
|
|
|
|
* array, on request, but reverse order, on response.
|
2013-02-26 09:22:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
var interceptorFactories = this.interceptors = [];
|
2013-09-28 03:53:36 +00:00
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
/**
|
2013-09-28 03:53:36 +00:00
|
|
|
|
* For historical reasons, response interceptors are ordered by the order in which
|
|
|
|
|
|
* they are applied to the response. (This is the opposite of interceptorFactories)
|
2013-02-26 09:22:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
var responseInterceptorFactories = this.responseInterceptors = [];
|
2011-11-30 08:58:34 +00:00
|
|
|
|
|
2013-07-15 19:26:46 +00:00
|
|
|
|
this.$get = ['$httpBackend', '$browser', '$cacheFactory', '$rootScope', '$q', '$injector', '$$urlUtils',
|
|
|
|
|
|
function($httpBackend, $browser, $cacheFactory, $rootScope, $q, $injector, $$urlUtils) {
|
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
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
var defaultCache = $cacheFactory('$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
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Interceptors stored in reverse order. Inner interceptors before outer interceptors.
|
|
|
|
|
|
* The reversal is needed so that we can build up the interception chain around the
|
|
|
|
|
|
* server request.
|
|
|
|
|
|
*/
|
|
|
|
|
|
var reversedInterceptors = [];
|
|
|
|
|
|
|
|
|
|
|
|
forEach(interceptorFactories, function(interceptorFactory) {
|
|
|
|
|
|
reversedInterceptors.unshift(isString(interceptorFactory)
|
|
|
|
|
|
? $injector.get(interceptorFactory) : $injector.invoke(interceptorFactory));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
forEach(responseInterceptorFactories, function(interceptorFactory, index) {
|
|
|
|
|
|
var responseFn = isString(interceptorFactory)
|
|
|
|
|
|
? $injector.get(interceptorFactory)
|
|
|
|
|
|
: $injector.invoke(interceptorFactory);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Response interceptors go before "around" interceptors (no real reason, just
|
2013-07-22 15:01:00 +00:00
|
|
|
|
* had to pick one.) But they are already reversed, so we can't use unshift, hence
|
2013-02-26 09:22:12 +00:00
|
|
|
|
* the splice.
|
|
|
|
|
|
*/
|
|
|
|
|
|
reversedInterceptors.splice(index, 0, {
|
|
|
|
|
|
response: function(response) {
|
|
|
|
|
|
return responseFn($q.when(response));
|
|
|
|
|
|
},
|
|
|
|
|
|
responseError: function(response) {
|
|
|
|
|
|
return responseFn($q.reject(response));
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2011-12-28 17:26:22 +00:00
|
|
|
|
});
|
2011-12-01 23:54:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc function
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http
|
2012-12-04 19:56:16 +00:00
|
|
|
|
* @requires $httpBackend
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* @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
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* @description
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* The `$http` service is a core Angular service that facilitates communication with the remote
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* HTTP servers via the browser's {@link https://developer.mozilla.org/en/xmlhttprequest
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* XMLHttpRequest} object or via {@link http://en.wikipedia.org/wiki/JSONP JSONP}.
|
|
|
|
|
|
*
|
|
|
|
|
|
* For unit testing applications that use `$http` service, see
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* {@link ngMock.$httpBackend $httpBackend mock}.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* For a higher level of abstraction, please check out the {@link ngResource.$resource
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* $resource} service.
|
|
|
|
|
|
*
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* The $http API is based on the {@link ng.$q deferred/promise APIs} exposed by
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* the $q service. While for simple usage patterns this doesn't matter much, for advanced usage
|
|
|
|
|
|
* it is important to familiarize yourself with these APIs and the guarantees they provide.
|
2012-01-19 20:39:05 +00:00
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* # General usage
|
|
|
|
|
|
* The `$http` service is a function which takes a single argument — a configuration object —
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* that is used to generate an HTTP request and returns a {@link ng.$q promise}
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* with two $http specific methods: `success` and `error`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* $http({method: 'GET', url: '/someUrl'}).
|
|
|
|
|
|
* success(function(data, status, headers, config) {
|
|
|
|
|
|
* // this callback will be called asynchronously
|
|
|
|
|
|
* // when the response is available
|
|
|
|
|
|
* }).
|
|
|
|
|
|
* error(function(data, status, headers, config) {
|
|
|
|
|
|
* // called asynchronously if an error occurs
|
2012-12-31 12:03:49 +00:00
|
|
|
|
* // or server returns response with an error status.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* });
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Since the returned value of calling the $http function is a `promise`, you can also use
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* the `then` method to register callbacks, and these callbacks will receive a single argument –
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* an object representing the response. See the API signature and type info below for more
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* details.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* A response status code between 200 and 299 is considered a success status and
|
2012-12-31 12:03:49 +00:00
|
|
|
|
* will result in the success callback being called. Note that if the response is a redirect,
|
|
|
|
|
|
* XMLHttpRequest will transparently follow it, meaning that the error callback will not be
|
|
|
|
|
|
* called for such responses.
|
2013-09-13 10:36:27 +00:00
|
|
|
|
*
|
2013-09-13 13:17:47 +00:00
|
|
|
|
* # Calling $http from outside AngularJS
|
|
|
|
|
|
* The `$http` service will not actually send the request until the next `$digest()` is executed.
|
|
|
|
|
|
* Normally this is not an issue, since almost all the time your call to `$http` will be from within
|
|
|
|
|
|
* a `$apply()` block.
|
|
|
|
|
|
* If you are calling `$http` from outside Angular, then you should wrap it in a call to `$apply`
|
|
|
|
|
|
* to cause a $digest to occur and also to handle errors in the block correctly.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```
|
|
|
|
|
|
* $scope.$apply(function() {
|
|
|
|
|
|
* $http(...);
|
|
|
|
|
|
* });
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* # Writing Unit Tests that use $http
|
|
|
|
|
|
* When unit testing you are mostly responsible for scheduling the `$digest` cycle. If you do not
|
|
|
|
|
|
* trigger a `$digest` before calling `$httpBackend.flush()` then the request will not have been
|
|
|
|
|
|
* made and `$httpBackend.expect(...)` expectations will fail. The solution is to run the code
|
|
|
|
|
|
* that calls the `$http()` method inside a $apply block as explained in the previous section.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```
|
|
|
|
|
|
* $httpBackend.expectGET(...);
|
|
|
|
|
|
* $scope.$apply(function() {
|
|
|
|
|
|
* $http.get(...);
|
|
|
|
|
|
* });
|
|
|
|
|
|
* $httpBackend.flush();
|
|
|
|
|
|
* ```
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* # Shortcut methods
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Since all invocations of the $http service require passing in an HTTP method and URL, and
|
|
|
|
|
|
* POST/PUT requests require request data to be provided as well, shortcut methods
|
|
|
|
|
|
* were created:
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* $http.get('/someUrl').success(successCallback);
|
|
|
|
|
|
* $http.post('/someUrl', data).success(successCallback);
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* Complete list of shortcut methods:
|
|
|
|
|
|
*
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* - {@link ng.$http#get $http.get}
|
|
|
|
|
|
* - {@link ng.$http#head $http.head}
|
|
|
|
|
|
* - {@link ng.$http#post $http.post}
|
|
|
|
|
|
* - {@link ng.$http#put $http.put}
|
|
|
|
|
|
* - {@link ng.$http#delete $http.delete}
|
|
|
|
|
|
* - {@link ng.$http#jsonp $http.jsonp}
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
*
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* # Setting HTTP Headers
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* The $http service will automatically add certain HTTP headers to all requests. These defaults
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* can be fully configured by accessing the `$httpProvider.defaults.headers` configuration
|
|
|
|
|
|
* object, which currently contains this default configuration:
|
|
|
|
|
|
*
|
|
|
|
|
|
* - `$httpProvider.defaults.headers.common` (headers that are common for all requests):
|
|
|
|
|
|
* - `Accept: application/json, text/plain, * / *`
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* - `$httpProvider.defaults.headers.post`: (header defaults for POST requests)
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* - `Content-Type: application/json`
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* - `$httpProvider.defaults.headers.put` (header defaults for PUT requests)
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* - `Content-Type: application/json`
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* To add or overwrite these defaults, simply add or remove a property from these configuration
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* objects. To add headers for an HTTP method other than POST or PUT, simply add a new object
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* with the lowercased HTTP method name as the key, e.g.
|
2013-09-22 12:34:01 +00:00
|
|
|
|
* `$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Additionally, the defaults can be set at runtime via the `$http.defaults` object in the same
|
|
|
|
|
|
* fashion.
|
2012-04-11 06:20:53 +00:00
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* # Transforming Requests and Responses
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* Both requests and responses can be transformed using transform functions. By default, Angular
|
|
|
|
|
|
* applies these transformations:
|
|
|
|
|
|
*
|
|
|
|
|
|
* Request transformations:
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* - If the `data` property of the request configuration object contains an object, serialize it into
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* JSON format.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Response transformations:
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
|
|
|
|
|
|
* - If JSON response is detected, deserialize it using a JSON parser.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-03-06 19:20:30 +00:00
|
|
|
|
* To globally augment or override the default transforms, modify the `$httpProvider.defaults.transformRequest` and
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* `$httpProvider.defaults.transformResponse` properties. These properties are by default an
|
2013-03-06 19:20:30 +00:00
|
|
|
|
* array of transform functions, which allows you to `push` or `unshift` a new transformation function into the
|
|
|
|
|
|
* transformation chain. You can also decide to completely override any default transformations by assigning your
|
|
|
|
|
|
* transformation functions to these properties directly without the array wrapper.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Similarly, to locally override the request/response transforms, augment the `transformRequest` and/or
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* `transformResponse` properties of the configuration object passed into `$http`.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* # Caching
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* To enable caching, set the configuration property `cache` to `true`. When the cache is
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* enabled, `$http` stores the response from the server in local cache. Next time the
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* response is served from the cache without sending a request to the server.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Note that even if the response is served from cache, delivery of the data is asynchronous in
|
|
|
|
|
|
* the same way that real requests are.
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* If there are multiple GET requests for the same URL that should be cached using the same
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* cache, but the cache is not populated yet, only one request to the server will be made and
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* the remaining requests will be fulfilled using the response from the first request.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-02-28 05:25:21 +00:00
|
|
|
|
* A custom default cache built with $cacheFactory can be provided in $http.defaults.cache.
|
|
|
|
|
|
* To skip it, set configuration property `cache` to `false`.
|
2013-05-11 16:23:33 +00:00
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-02-26 09:22:12 +00:00
|
|
|
|
* # Interceptors
|
|
|
|
|
|
*
|
|
|
|
|
|
* Before you start creating interceptors, be sure to understand the
|
|
|
|
|
|
* {@link ng.$q $q and deferred/promise APIs}.
|
|
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* For purposes of global error handling, authentication, or any kind of synchronous or
|
2013-02-26 09:22:12 +00:00
|
|
|
|
* asynchronous pre-processing of request or postprocessing of responses, it is desirable to be
|
|
|
|
|
|
* able to intercept requests before they are handed to the server and
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* responses before they are handed over to the application code that
|
2013-02-26 09:22:12 +00:00
|
|
|
|
* initiated these requests. The interceptors leverage the {@link ng.$q
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* promise APIs} to fulfill this need for both synchronous and asynchronous pre-processing.
|
2013-02-26 09:22:12 +00:00
|
|
|
|
*
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* The interceptors are service factories that are registered with the `$httpProvider` by
|
2013-02-26 09:22:12 +00:00
|
|
|
|
* adding them to the `$httpProvider.interceptors` array. The factory is called and
|
|
|
|
|
|
* injected with dependencies (if specified) and returns the interceptor.
|
|
|
|
|
|
*
|
|
|
|
|
|
* There are two kinds of interceptors (and two kinds of rejection interceptors):
|
|
|
|
|
|
*
|
|
|
|
|
|
* * `request`: interceptors get called with http `config` object. The function is free to modify
|
|
|
|
|
|
* the `config` or create a new one. The function needs to return the `config` directly or as a
|
|
|
|
|
|
* promise.
|
|
|
|
|
|
* * `requestError`: interceptor gets called when a previous interceptor threw an error or resolved
|
|
|
|
|
|
* with a rejection.
|
|
|
|
|
|
* * `response`: interceptors get called with http `response` object. The function is free to modify
|
|
|
|
|
|
* the `response` or create a new one. The function needs to return the `response` directly or as a
|
|
|
|
|
|
* promise.
|
|
|
|
|
|
* * `responseError`: interceptor gets called when a previous interceptor threw an error or resolved
|
|
|
|
|
|
* with a rejection.
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* // register the interceptor as a service
|
|
|
|
|
|
* $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
|
|
|
|
|
|
* return {
|
|
|
|
|
|
* // optional method
|
|
|
|
|
|
* 'request': function(config) {
|
|
|
|
|
|
* // do something on success
|
|
|
|
|
|
* return config || $q.when(config);
|
|
|
|
|
|
* },
|
|
|
|
|
|
*
|
|
|
|
|
|
* // optional method
|
|
|
|
|
|
* 'requestError': function(rejection) {
|
|
|
|
|
|
* // do something on error
|
|
|
|
|
|
* if (canRecover(rejection)) {
|
|
|
|
|
|
* return responseOrNewPromise
|
|
|
|
|
|
* }
|
|
|
|
|
|
* return $q.reject(rejection);
|
|
|
|
|
|
* },
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* // optional method
|
|
|
|
|
|
* 'response': function(response) {
|
|
|
|
|
|
* // do something on success
|
|
|
|
|
|
* return response || $q.when(response);
|
|
|
|
|
|
* },
|
|
|
|
|
|
*
|
|
|
|
|
|
* // optional method
|
|
|
|
|
|
* 'responseError': function(rejection) {
|
|
|
|
|
|
* // do something on error
|
|
|
|
|
|
* if (canRecover(rejection)) {
|
|
|
|
|
|
* return responseOrNewPromise
|
|
|
|
|
|
* }
|
|
|
|
|
|
* return $q.reject(rejection);
|
|
|
|
|
|
* };
|
|
|
|
|
|
* }
|
|
|
|
|
|
* });
|
|
|
|
|
|
*
|
|
|
|
|
|
* $httpProvider.interceptors.push('myHttpInterceptor');
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* // register the interceptor via an anonymous factory
|
|
|
|
|
|
* $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
|
|
|
|
|
|
* return {
|
|
|
|
|
|
* 'request': function(config) {
|
|
|
|
|
|
* // same as above
|
|
|
|
|
|
* },
|
|
|
|
|
|
* 'response': function(response) {
|
|
|
|
|
|
* // same as above
|
|
|
|
|
|
* }
|
|
|
|
|
|
* });
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* # Response interceptors (DEPRECATED)
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* Before you start creating interceptors, be sure to understand the
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* {@link ng.$q $q and deferred/promise APIs}.
|
2012-01-19 20:39:05 +00:00
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* For purposes of global error handling, authentication or any kind of synchronous or
|
|
|
|
|
|
* asynchronous preprocessing of received responses, it is desirable to be able to intercept
|
|
|
|
|
|
* responses for http requests before they are handed over to the application code that
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* initiated these requests. The response interceptors leverage the {@link ng.$q
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* promise apis} to fulfil this need for both synchronous and asynchronous preprocessing.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The interceptors are service factories that are registered with the $httpProvider by
|
|
|
|
|
|
* adding them to the `$httpProvider.responseInterceptors` array. The factory is called and
|
|
|
|
|
|
* injected with dependencies (if specified) and returns the interceptor — a function that
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* takes a {@link ng.$q promise} and returns the original or a new promise.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* // register the interceptor as a service
|
|
|
|
|
|
* $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
|
|
|
|
|
|
* return function(promise) {
|
|
|
|
|
|
* return promise.then(function(response) {
|
|
|
|
|
|
* // do something on success
|
2013-08-14 18:25:01 +00:00
|
|
|
|
* return response;
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* }, function(response) {
|
|
|
|
|
|
* // do something on error
|
|
|
|
|
|
* if (canRecover(response)) {
|
|
|
|
|
|
* return responseOrNewPromise
|
|
|
|
|
|
* }
|
|
|
|
|
|
* return $q.reject(response);
|
|
|
|
|
|
* });
|
|
|
|
|
|
* }
|
|
|
|
|
|
* });
|
|
|
|
|
|
*
|
|
|
|
|
|
* $httpProvider.responseInterceptors.push('myHttpInterceptor');
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* // register the interceptor via an anonymous factory
|
|
|
|
|
|
* $httpProvider.responseInterceptors.push(function($q, dependency1, dependency2) {
|
|
|
|
|
|
* return function(promise) {
|
|
|
|
|
|
* // same as above
|
|
|
|
|
|
* }
|
|
|
|
|
|
* });
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* # Security Considerations
|
|
|
|
|
|
*
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* When designing web applications, consider security threats from:
|
|
|
|
|
|
*
|
|
|
|
|
|
* - {@link http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* JSON vulnerability}
|
2012-01-19 20:39:05 +00:00
|
|
|
|
* - {@link http://en.wikipedia.org/wiki/Cross-site_request_forgery XSRF}
|
|
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* Both server and the client must cooperate in order to eliminate these threats. Angular comes
|
|
|
|
|
|
* pre-configured with strategies that address these issues, but for this to work backend server
|
|
|
|
|
|
* cooperation is required.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## JSON Vulnerability Protection
|
|
|
|
|
|
*
|
|
|
|
|
|
* A {@link http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* JSON vulnerability} allows third party website to turn your JSON resource URL into
|
|
|
|
|
|
* {@link http://en.wikipedia.org/wiki/JSONP JSONP} request under some conditions. To
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* counter this your server can prefix all JSON requests with following string `")]}',\n"`.
|
|
|
|
|
|
* Angular will automatically strip the prefix before processing it as JSON.
|
|
|
|
|
|
*
|
|
|
|
|
|
* For example if your server needs to return:
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* ['one','two']
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* which is vulnerable to attack, your server can return:
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* )]}',
|
|
|
|
|
|
* ['one','two']
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* Angular will strip the prefix, before processing the JSON.
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## Cross Site Request Forgery (XSRF) Protection
|
|
|
|
|
|
*
|
|
|
|
|
|
* {@link http://en.wikipedia.org/wiki/Cross-site_request_forgery XSRF} is a technique by which
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* an unauthorized site can gain your user's private data. Angular provides a mechanism
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie
|
2013-02-05 12:37:36 +00:00
|
|
|
|
* (by default, `XSRF-TOKEN`) and sets it as an HTTP header (`X-XSRF-TOKEN`). Since only
|
|
|
|
|
|
* JavaScript that runs on your domain could read the cookie, your server can be assured that
|
|
|
|
|
|
* the XHR came from JavaScript running on your domain. The header will not be set for
|
|
|
|
|
|
* cross-domain requests.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* To take advantage of this, your server needs to set a token in a JavaScript readable session
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* cookie called `XSRF-TOKEN` on the first HTTP GET request. On subsequent XHR requests the
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* server can verify that the cookie matches `X-XSRF-TOKEN` HTTP header, and therefore be sure
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* that only JavaScript running on your domain could have sent the request. The token must be
|
|
|
|
|
|
* unique for each user and must be verifiable by the server (to prevent the JavaScript from making
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* up its own tokens). We recommend that the token is a digest of your site's authentication
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* cookie with a {@link https://en.wikipedia.org/wiki/Salt_(cryptography) salt} for added security.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
2013-02-05 12:37:36 +00:00
|
|
|
|
* The name of the headers can be specified using the xsrfHeaderName and xsrfCookieName
|
|
|
|
|
|
* properties of either $httpProvider.defaults, or the per-request config object.
|
|
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {object} config Object describing the request to be made and how it should be
|
|
|
|
|
|
* processed. The object has following properties:
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
|
|
|
|
|
|
* - **url** – `{string}` – Absolute or relative URL of the resource that is being requested.
|
2012-03-23 20:41:48 +00:00
|
|
|
|
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
|
|
|
|
|
|
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* - **data** – `{string|Object}` – Data to be sent as the request message data.
|
2013-07-03 07:51:05 +00:00
|
|
|
|
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
|
|
|
|
|
|
* HTTP headers to send to the server. If the return value of a function is null, the header will
|
|
|
|
|
|
* not be sent.
|
2013-02-05 12:37:36 +00:00
|
|
|
|
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
|
|
|
|
|
|
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
|
|
|
|
|
* transform function or an array of such functions. The transform function takes the http
|
|
|
|
|
|
* request body and headers and returns its transformed (typically serialized) version.
|
|
|
|
|
|
* - **transformResponse** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
|
|
|
|
|
* transform function or an array of such functions. The transform function takes the http
|
|
|
|
|
|
* response body and headers and returns its transformed (typically deserialized) version.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* GET request, otherwise if a cache instance built with
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* caching.
|
2013-04-27 15:22:03 +00:00
|
|
|
|
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
|
|
|
|
|
|
* that should abort the request when resolved.
|
2012-04-03 18:00:52 +00:00
|
|
|
|
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
|
|
|
|
|
|
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
|
|
|
|
|
|
* requests with credentials} for more information.
|
2012-08-04 19:11:00 +00:00
|
|
|
|
* - **responseType** - `{string}` - see {@link
|
|
|
|
|
|
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* 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:
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* - **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.
|
|
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
* @property {Array.<Object>} pendingRequests Array of config objects for currently pending
|
|
|
|
|
|
* requests. This is primarily meant to be used for debugging purposes.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
2012-01-11 03:16:33 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @example
|
2012-04-29 05:45:28 +00:00
|
|
|
|
<example>
|
|
|
|
|
|
<file name="index.html">
|
|
|
|
|
|
<div ng-controller="FetchCtrl">
|
|
|
|
|
|
<select ng-model="method">
|
|
|
|
|
|
<option>GET</option>
|
|
|
|
|
|
<option>JSONP</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
<input type="text" ng-model="url" size="80"/>
|
|
|
|
|
|
<button ng-click="fetch()">fetch</button><br>
|
|
|
|
|
|
<button ng-click="updateModel('GET', 'http-hello.html')">Sample GET</button>
|
|
|
|
|
|
<button ng-click="updateModel('JSONP', 'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero')">Sample JSONP</button>
|
|
|
|
|
|
<button ng-click="updateModel('JSONP', 'http://angularjs.org/doesntexist&callback=JSON_CALLBACK')">Invalid JSONP</button>
|
|
|
|
|
|
<pre>http status code: {{status}}</pre>
|
|
|
|
|
|
<pre>http response data: {{data}}</pre>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</file>
|
|
|
|
|
|
<file name="script.js">
|
|
|
|
|
|
function FetchCtrl($scope, $http, $templateCache) {
|
|
|
|
|
|
$scope.method = 'GET';
|
|
|
|
|
|
$scope.url = 'http-hello.html';
|
|
|
|
|
|
|
|
|
|
|
|
$scope.fetch = function() {
|
|
|
|
|
|
$scope.code = null;
|
|
|
|
|
|
$scope.response = null;
|
|
|
|
|
|
|
|
|
|
|
|
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
|
|
|
|
|
|
success(function(data, status) {
|
|
|
|
|
|
$scope.status = status;
|
|
|
|
|
|
$scope.data = data;
|
|
|
|
|
|
}).
|
|
|
|
|
|
error(function(data, status) {
|
|
|
|
|
|
$scope.data = data || "Request failed";
|
|
|
|
|
|
$scope.status = status;
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$scope.updateModel = function(method, url) {
|
|
|
|
|
|
$scope.method = method;
|
|
|
|
|
|
$scope.url = url;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
</file>
|
|
|
|
|
|
<file name="http-hello.html">
|
|
|
|
|
|
Hello, $http!
|
|
|
|
|
|
</file>
|
|
|
|
|
|
<file name="scenario.js">
|
|
|
|
|
|
it('should make an xhr GET request', function() {
|
|
|
|
|
|
element(':button:contains("Sample GET")').click();
|
|
|
|
|
|
element(':button:contains("fetch")').click();
|
|
|
|
|
|
expect(binding('status')).toBe('200');
|
|
|
|
|
|
expect(binding('data')).toMatch(/Hello, \$http!/);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should make a JSONP request to angularjs.org', function() {
|
|
|
|
|
|
element(':button:contains("Sample JSONP")').click();
|
|
|
|
|
|
element(':button:contains("fetch")').click();
|
|
|
|
|
|
expect(binding('status')).toBe('200');
|
|
|
|
|
|
expect(binding('data')).toMatch(/Super Hero!/);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should make JSONP request to invalid URL and invoke the error handler',
|
|
|
|
|
|
function() {
|
|
|
|
|
|
element(':button:contains("Invalid JSONP")').click();
|
|
|
|
|
|
element(':button:contains("fetch")').click();
|
|
|
|
|
|
expect(binding('status')).toBe('0');
|
|
|
|
|
|
expect(binding('data')).toBe('Request failed');
|
|
|
|
|
|
});
|
|
|
|
|
|
</file>
|
|
|
|
|
|
</example>
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*/
|
2013-02-26 09:22:12 +00:00
|
|
|
|
function $http(requestConfig) {
|
|
|
|
|
|
var config = {
|
|
|
|
|
|
transformRequest: defaults.transformRequest,
|
|
|
|
|
|
transformResponse: defaults.transformResponse
|
|
|
|
|
|
};
|
2013-06-19 20:30:28 +00:00
|
|
|
|
var headers = mergeHeaders(requestConfig);
|
2013-02-26 09:22:12 +00:00
|
|
|
|
|
|
|
|
|
|
extend(config, requestConfig);
|
|
|
|
|
|
config.headers = headers;
|
2011-12-28 17:26:22 +00:00
|
|
|
|
config.method = uppercase(config.method);
|
2011-11-30 08:58:34 +00:00
|
|
|
|
|
2013-07-15 19:26:46 +00:00
|
|
|
|
var xsrfValue = $$urlUtils.isSameOrigin(config.url)
|
2013-02-26 09:22:12 +00:00
|
|
|
|
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
|
|
|
|
|
|
: undefined;
|
|
|
|
|
|
if (xsrfValue) {
|
|
|
|
|
|
headers[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
|
2012-07-30 00:52:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
var serverRequest = function(config) {
|
2013-07-13 00:42:37 +00:00
|
|
|
|
headers = config.headers;
|
2013-02-26 09:22:12 +00:00
|
|
|
|
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
|
|
|
|
|
|
|
|
|
|
|
|
// strip content-type if data is undefined
|
|
|
|
|
|
if (isUndefined(config.data)) {
|
2013-06-19 20:30:28 +00:00
|
|
|
|
forEach(headers, function(value, header) {
|
|
|
|
|
|
if (lowercase(header) === 'content-type') {
|
|
|
|
|
|
delete headers[header];
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2013-02-26 09:22:12 +00:00
|
|
|
|
}
|
2011-11-24 11:53:04 +00:00
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) {
|
|
|
|
|
|
config.withCredentials = defaults.withCredentials;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// send request
|
|
|
|
|
|
return sendReq(config, reqData, headers).then(transformResponse, transformResponse);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var chain = [serverRequest, undefined];
|
|
|
|
|
|
var promise = $q.when(config);
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
// apply interceptors
|
2013-02-26 09:22:12 +00:00
|
|
|
|
forEach(reversedInterceptors, function(interceptor) {
|
|
|
|
|
|
if (interceptor.request || interceptor.requestError) {
|
|
|
|
|
|
chain.unshift(interceptor.request, interceptor.requestError);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (interceptor.response || interceptor.responseError) {
|
|
|
|
|
|
chain.push(interceptor.response, interceptor.responseError);
|
|
|
|
|
|
}
|
2011-12-28 17:26:22 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2013-02-26 09:22:12 +00:00
|
|
|
|
while(chain.length) {
|
|
|
|
|
|
var thenFn = chain.shift();
|
|
|
|
|
|
var rejectFn = chain.shift();
|
|
|
|
|
|
|
|
|
|
|
|
promise = promise.then(thenFn, rejectFn);
|
2013-05-07 04:56:51 +00:00
|
|
|
|
}
|
2013-02-26 09:22:12 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
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, {
|
2013-02-26 09:22:12 +00:00
|
|
|
|
data: transformData(response.data, response.headers, config.transformResponse)
|
2011-12-28 17:26:22 +00:00
|
|
|
|
});
|
|
|
|
|
|
return (isSuccess(response.status))
|
|
|
|
|
|
? resp
|
|
|
|
|
|
: $q.reject(resp);
|
|
|
|
|
|
}
|
2013-06-19 20:30:28 +00:00
|
|
|
|
|
|
|
|
|
|
function mergeHeaders(config) {
|
|
|
|
|
|
var defHeaders = defaults.headers,
|
|
|
|
|
|
reqHeaders = extend({}, config.headers),
|
|
|
|
|
|
defHeaderName, lowercaseDefHeaderName, reqHeaderName;
|
|
|
|
|
|
|
|
|
|
|
|
defHeaders = extend({}, defHeaders.common, defHeaders[lowercase(config.method)]);
|
|
|
|
|
|
|
2013-07-03 07:51:05 +00:00
|
|
|
|
// execute if header value is function
|
|
|
|
|
|
execHeaders(defHeaders);
|
|
|
|
|
|
execHeaders(reqHeaders);
|
|
|
|
|
|
|
2013-06-19 20:30:28 +00:00
|
|
|
|
// using for-in instead of forEach to avoid unecessary iteration after header has been found
|
|
|
|
|
|
defaultHeadersIteration:
|
|
|
|
|
|
for (defHeaderName in defHeaders) {
|
|
|
|
|
|
lowercaseDefHeaderName = lowercase(defHeaderName);
|
|
|
|
|
|
|
|
|
|
|
|
for (reqHeaderName in reqHeaders) {
|
|
|
|
|
|
if (lowercase(reqHeaderName) === lowercaseDefHeaderName) {
|
|
|
|
|
|
continue defaultHeadersIteration;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reqHeaders[defHeaderName] = defHeaders[defHeaderName];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return reqHeaders;
|
2013-07-03 07:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
function execHeaders(headers) {
|
|
|
|
|
|
var headerContent;
|
|
|
|
|
|
|
|
|
|
|
|
forEach(headers, function(headerFn, header) {
|
|
|
|
|
|
if (isFunction(headerFn)) {
|
|
|
|
|
|
headerContent = headerFn();
|
|
|
|
|
|
if (headerContent != null) {
|
|
|
|
|
|
headers[header] = headerContent;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
delete headers[header];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2013-06-19 20:30:28 +00:00
|
|
|
|
}
|
2011-12-28 17:26:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#get
|
|
|
|
|
|
* @methodOf ng.$http
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Shortcut method to perform `GET` request.
|
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 {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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#delete
|
|
|
|
|
|
* @methodOf ng.$http
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Shortcut method to perform `DELETE` request.
|
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 {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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#head
|
|
|
|
|
|
* @methodOf 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
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* 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
|
2012-04-11 06:30:22 +00:00
|
|
|
|
* @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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#jsonp
|
|
|
|
|
|
* @methodOf 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
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* 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
|
2012-04-11 06:30:22 +00:00
|
|
|
|
* @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
|
|
|
|
*/
|
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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#post
|
|
|
|
|
|
* @methodOf ng.$http
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Shortcut method to perform `POST` request.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @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
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#put
|
|
|
|
|
|
* @methodOf ng.$http
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
2013-03-29 23:36:33 +00:00
|
|
|
|
* Shortcut method to perform `PUT` request.
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
|
|
|
|
* @param {*} data Request content
|
|
|
|
|
|
* @param {Object=} config Optional configuration object
|
2012-04-11 06:30:22 +00:00
|
|
|
|
* @returns {HttpPromise} Future object
|
2011-12-28 17:26:22 +00:00
|
|
|
|
*/
|
|
|
|
|
|
createShortMethodsWithData('post', 'put');
|
|
|
|
|
|
|
2012-04-11 06:20:53 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @ngdoc property
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ng.$http#defaults
|
|
|
|
|
|
* @propertyOf ng.$http
|
2012-04-11 06:20:53 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
|
2012-07-30 00:52:19 +00:00
|
|
|
|
* default headers, withCredentials as well as request and response transformations.
|
2012-04-11 06:20:53 +00:00
|
|
|
|
*
|
|
|
|
|
|
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
|
|
|
|
|
|
*/
|
2012-07-30 00:52:19 +00:00
|
|
|
|
$http.defaults = defaults;
|
2012-04-11 06:20:53 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
/**
|
2013-03-29 23:36:33 +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:
|
2012-07-30 00:52:19 +00:00
|
|
|
|
* $httpBackend, defaults, $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,
|
2012-03-23 20:41:48 +00:00
|
|
|
|
cachedResp,
|
|
|
|
|
|
url = buildUrl(config.url, config.params);
|
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
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 05:25:21 +00:00
|
|
|
|
if ((config.cache || defaults.cache) && config.cache !== false && config.method == 'GET') {
|
2013-05-11 16:23:33 +00:00
|
|
|
|
cache = isObject(config.cache) ? config.cache
|
|
|
|
|
|
: isObject(defaults.cache) ? defaults.cache
|
2013-02-28 05:25:21 +00:00
|
|
|
|
: defaultCache;
|
2011-12-28 17:26:22 +00:00
|
|
|
|
}
|
2011-11-04 23:34:47 +00:00
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
if (cache) {
|
2012-03-23 20:41:48 +00:00
|
|
|
|
cachedResp = cache.get(url);
|
2013-08-30 01:52:30 +00:00
|
|
|
|
if (isDefined(cachedResp)) {
|
2011-12-28 17:26:22 +00:00
|
|
|
|
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
|
2012-03-23 20:41:48 +00:00
|
|
|
|
cache.put(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
|
2013-08-30 01:52:30 +00:00
|
|
|
|
if (isUndefined(cachedResp)) {
|
2012-04-03 18:00:52 +00:00
|
|
|
|
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
|
2012-08-04 19:11:00 +00:00
|
|
|
|
config.withCredentials, config.responseType);
|
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)) {
|
2012-03-23 20:41:48 +00:00
|
|
|
|
cache.put(url, [status, response, parseHeaders(headersString)]);
|
2011-12-28 17:26:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
// remove promise from the cache
|
2012-03-23 20:41:48 +00:00
|
|
|
|
cache.remove(url);
|
2011-12-28 17:26:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolvePromise(response, status, headersString);
|
2013-04-27 15:22:03 +00:00
|
|
|
|
if (!$rootScope.$$phase) $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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-03-23 20:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function buildUrl(url, params) {
|
|
|
|
|
|
if (!params) return url;
|
|
|
|
|
|
var parts = [];
|
|
|
|
|
|
forEachSorted(params, function(value, key) {
|
|
|
|
|
|
if (value == null || value == undefined) return;
|
2012-09-14 21:42:39 +00:00
|
|
|
|
if (!isArray(value)) value = [value];
|
|
|
|
|
|
|
|
|
|
|
|
forEach(value, function(v) {
|
|
|
|
|
|
if (isObject(v)) {
|
|
|
|
|
|
v = toJson(v);
|
|
|
|
|
|
}
|
2013-02-14 22:39:55 +00:00
|
|
|
|
parts.push(encodeUriQuery(key) + '=' +
|
|
|
|
|
|
encodeUriQuery(v));
|
2012-09-14 21:42:39 +00:00
|
|
|
|
});
|
2012-03-23 20:41:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 17:26:22 +00:00
|
|
|
|
}];
|
|
|
|
|
|
}
|