($browser, createXhr, $browserDefer, callbacks, rawDocument)
| 8539 | } |
| 8540 | |
| 8541 | function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) { |
| 8542 | var ABORTED = -1; |
| 8543 | |
| 8544 | // TODO(vojta): fix the signature |
| 8545 | return function(method, url, post, callback, headers, timeout, withCredentials, responseType) { |
| 8546 | var status; |
| 8547 | $browser.$$incOutstandingRequestCount(); |
| 8548 | url = url || $browser.url(); |
| 8549 | |
| 8550 | if (lowercase(method) == 'jsonp') { |
| 8551 | var callbackId = '_' + (callbacks.counter++).toString(36); |
| 8552 | callbacks[callbackId] = function(data) { |
| 8553 | callbacks[callbackId].data = data; |
| 8554 | callbacks[callbackId].called = true; |
| 8555 | }; |
| 8556 | |
| 8557 | var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), |
| 8558 | callbackId, function(status, text) { |
| 8559 | completeRequest(callback, status, callbacks[callbackId].data, "", text); |
| 8560 | callbacks[callbackId] = noop; |
| 8561 | }); |
| 8562 | } else { |
| 8563 | |
| 8564 | var xhr = createXhr(method); |
| 8565 | |
| 8566 | xhr.open(method, url, true); |
| 8567 | forEach(headers, function(value, key) { |
| 8568 | if (isDefined(value)) { |
| 8569 | xhr.setRequestHeader(key, value); |
| 8570 | } |
| 8571 | }); |
| 8572 | |
| 8573 | // In IE6 and 7, this might be called synchronously when xhr.send below is called and the |
| 8574 | // response is in the cache. the promise api will ensure that to the app code the api is |
| 8575 | // always async |
| 8576 | xhr.onreadystatechange = function() { |
| 8577 | // onreadystatechange might get called multiple times with readyState === 4 on mobile webkit caused by |
| 8578 | // xhrs that are resolved while the app is in the background (see #5426). |
| 8579 | // since calling completeRequest sets the `xhr` variable to null, we just check if it's not null before |
| 8580 | // continuing |
| 8581 | // |
| 8582 | // we can't set xhr.onreadystatechange to undefined or delete it because that breaks IE8 (method=PATCH) and |
| 8583 | // Safari respectively. |
| 8584 | if (xhr && xhr.readyState == 4) { |
| 8585 | var responseHeaders = null, |
| 8586 | response = null, |
| 8587 | statusText = ''; |
| 8588 | |
| 8589 | if(status !== ABORTED) { |
| 8590 | responseHeaders = xhr.getAllResponseHeaders(); |
| 8591 | |
| 8592 | // responseText is the old-school way of retrieving response (supported by IE8 & 9) |
| 8593 | // response/responseType properties were introduced in XHR Level2 spec (supported by IE10) |
| 8594 | response = ('response' in xhr) ? xhr.response : xhr.responseText; |
| 8595 | } |
| 8596 | |
| 8597 | // Accessing statusText on an aborted xhr object will |
| 8598 | // throw an 'c00c023f error' in IE9 and lower, don't touch it. |
no test coverage detected