MCPcopy
hub / github.com/angular-ui/ui-router / createHttpBackend

Function createHttpBackend

test/angular/1.4/angular.js:10965–11110  ·  view source on GitHub ↗
($browser, createXhr, $browserDefer, callbacks, rawDocument)

Source from the content-addressed store, hash-verified

10963}
10964
10965function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
10966 // TODO(vojta): fix the signature
10967 return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
10968 $browser.$$incOutstandingRequestCount();
10969 url = url || $browser.url();
10970
10971 if (lowercase(method) == 'jsonp') {
10972 var callbackId = '_' + (callbacks.counter++).toString(36);
10973 callbacks[callbackId] = function(data) {
10974 callbacks[callbackId].data = data;
10975 callbacks[callbackId].called = true;
10976 };
10977
10978 var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
10979 callbackId, function(status, text) {
10980 completeRequest(callback, status, callbacks[callbackId].data, "", text);
10981 callbacks[callbackId] = noop;
10982 });
10983 } else {
10984
10985 var xhr = createXhr(method, url);
10986
10987 xhr.open(method, url, true);
10988 forEach(headers, function(value, key) {
10989 if (isDefined(value)) {
10990 xhr.setRequestHeader(key, value);
10991 }
10992 });
10993
10994 xhr.onload = function requestLoaded() {
10995 var statusText = xhr.statusText || '';
10996
10997 // responseText is the old-school way of retrieving response (supported by IE9)
10998 // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
10999 var response = ('response' in xhr) ? xhr.response : xhr.responseText;
11000
11001 // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
11002 var status = xhr.status === 1223 ? 204 : xhr.status;
11003
11004 // fix status code when it is 0 (0 status is undocumented).
11005 // Occurs when accessing file resources or on Android 4.1 stock browser
11006 // while retrieving files from application cache.
11007 if (status === 0) {
11008 status = response ? 200 : urlResolve(url).protocol == 'file' ? 404 : 0;
11009 }
11010
11011 completeRequest(callback,
11012 status,
11013 response,
11014 xhr.getAllResponseHeaders(),
11015 statusText);
11016 };
11017
11018 var requestError = function() {
11019 // The response is always empty
11020 // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
11021 completeRequest(callback, -1, null, null, '');
11022 };

Callers 1

$HttpBackendProviderFunction · 0.70

Calls 10

urlMethod · 0.80
lowercaseFunction · 0.70
jsonpReqFunction · 0.70
completeRequestFunction · 0.70
forEachFunction · 0.70
isDefinedFunction · 0.70
urlResolveFunction · 0.70
isUndefinedFunction · 0.70
isPromiseLikeFunction · 0.70
createXhrFunction · 0.50

Tested by

no test coverage detected