($browser, createXhr, $browserDefer, callbacks, rawDocument)
| 8568 | } |
| 8569 | |
| 8570 | function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) { |
| 8571 | var ABORTED = -1; |
| 8572 | |
| 8573 | // TODO(vojta): fix the signature |
| 8574 | return function(method, url, post, callback, headers, timeout, withCredentials, responseType) { |
| 8575 | var status; |
| 8576 | $browser.$$incOutstandingRequestCount(); |
| 8577 | url = url || $browser.url(); |
| 8578 | |
| 8579 | if (lowercase(method) == 'jsonp') { |
| 8580 | var callbackId = '_' + (callbacks.counter++).toString(36); |
| 8581 | callbacks[callbackId] = function(data) { |
| 8582 | callbacks[callbackId].data = data; |
| 8583 | callbacks[callbackId].called = true; |
| 8584 | }; |
| 8585 | |
| 8586 | var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), |
| 8587 | callbackId, function(status, text) { |
| 8588 | completeRequest(callback, status, callbacks[callbackId].data, "", text); |
| 8589 | callbacks[callbackId] = noop; |
| 8590 | }); |
| 8591 | } else { |
| 8592 | |
| 8593 | var xhr = createXhr(method); |
| 8594 | |
| 8595 | xhr.open(method, url, true); |
| 8596 | forEach(headers, function(value, key) { |
| 8597 | if (isDefined(value)) { |
| 8598 | xhr.setRequestHeader(key, value); |
| 8599 | } |
| 8600 | }); |
| 8601 | |
| 8602 | // In IE6 and 7, this might be called synchronously when xhr.send below is called and the |
| 8603 | // response is in the cache. the promise api will ensure that to the app code the api is |
| 8604 | // always async |
| 8605 | xhr.onreadystatechange = function() { |
| 8606 | // onreadystatechange might get called multiple times with readyState === 4 on mobile webkit caused by |
| 8607 | // xhrs that are resolved while the app is in the background (see #5426). |
| 8608 | // since calling completeRequest sets the `xhr` variable to null, we just check if it's not null before |
| 8609 | // continuing |
| 8610 | // |
| 8611 | // we can't set xhr.onreadystatechange to undefined or delete it because that breaks IE8 (method=PATCH) and |
| 8612 | // Safari respectively. |
| 8613 | if (xhr && xhr.readyState == 4) { |
| 8614 | var responseHeaders = null, |
| 8615 | response = null, |
| 8616 | statusText = ''; |
| 8617 | |
| 8618 | if(status !== ABORTED) { |
| 8619 | responseHeaders = xhr.getAllResponseHeaders(); |
| 8620 | |
| 8621 | // responseText is the old-school way of retrieving response (supported by IE8 & 9) |
| 8622 | // response/responseType properties were introduced in XHR Level2 spec (supported by IE10) |
| 8623 | response = ('response' in xhr) ? xhr.response : xhr.responseText; |
| 8624 | } |
| 8625 | |
| 8626 | // Accessing statusText on an aborted xhr object will |
| 8627 | // throw an 'c00c023f error' in IE9 and lower, don't touch it. |
no test coverage detected