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

Function createHttpBackend

test/angular/1.5/angular.js:12177–12332  ·  view source on GitHub ↗
($browser, createXhr, $browserDefer, callbacks, rawDocument)

Source from the content-addressed store, hash-verified

12175}
12176
12177function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
12178 // TODO(vojta): fix the signature
12179 return function(method, url, post, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) {
12180 $browser.$$incOutstandingRequestCount();
12181 url = url || $browser.url();
12182
12183 if (lowercase(method) === 'jsonp') {
12184 var callbackPath = callbacks.createCallback(url);
12185 var jsonpDone = jsonpReq(url, callbackPath, function(status, text) {
12186 // jsonpReq only ever sets status to 200 (OK), 404 (ERROR) or -1 (WAITING)
12187 var response = (status === 200) && callbacks.getResponse(callbackPath);
12188 completeRequest(callback, status, response, '', text);
12189 callbacks.removeCallback(callbackPath);
12190 });
12191 } else {
12192
12193 var xhr = createXhr(method, url);
12194
12195 xhr.open(method, url, true);
12196 forEach(headers, function(value, key) {
12197 if (isDefined(value)) {
12198 xhr.setRequestHeader(key, value);
12199 }
12200 });
12201
12202 xhr.onload = function requestLoaded() {
12203 var statusText = xhr.statusText || '';
12204
12205 // responseText is the old-school way of retrieving response (supported by IE9)
12206 // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
12207 var response = ('response' in xhr) ? xhr.response : xhr.responseText;
12208
12209 // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
12210 var status = xhr.status === 1223 ? 204 : xhr.status;
12211
12212 // fix status code when it is 0 (0 status is undocumented).
12213 // Occurs when accessing file resources or on Android 4.1 stock browser
12214 // while retrieving files from application cache.
12215 if (status === 0) {
12216 status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
12217 }
12218
12219 completeRequest(callback,
12220 status,
12221 response,
12222 xhr.getAllResponseHeaders(),
12223 statusText);
12224 };
12225
12226 var requestError = function() {
12227 // The response is always empty
12228 // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
12229 completeRequest(callback, -1, null, null, '');
12230 };
12231
12232 xhr.onerror = requestError;
12233 xhr.onabort = requestError;
12234 xhr.ontimeout = requestError;

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