($browser, createXhr, $browserDefer, callbacks, rawDocument)
| 12737 | } |
| 12738 | |
| 12739 | function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) { |
| 12740 | // TODO(vojta): fix the signature |
| 12741 | return function(method, url, post, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) { |
| 12742 | url = url || $browser.url(); |
| 12743 | |
| 12744 | if (lowercase(method) === 'jsonp') { |
| 12745 | var callbackPath = callbacks.createCallback(url); |
| 12746 | var jsonpDone = jsonpReq(url, callbackPath, function(status, text) { |
| 12747 | // jsonpReq only ever sets status to 200 (OK), 404 (ERROR) or -1 (WAITING) |
| 12748 | var response = (status === 200) && callbacks.getResponse(callbackPath); |
| 12749 | completeRequest(callback, status, response, '', text, 'complete'); |
| 12750 | callbacks.removeCallback(callbackPath); |
| 12751 | }); |
| 12752 | } else { |
| 12753 | |
| 12754 | var xhr = createXhr(method, url); |
| 12755 | |
| 12756 | xhr.open(method, url, true); |
| 12757 | forEach(headers, function(value, key) { |
| 12758 | if (isDefined(value)) { |
| 12759 | xhr.setRequestHeader(key, value); |
| 12760 | } |
| 12761 | }); |
| 12762 | |
| 12763 | xhr.onload = function requestLoaded() { |
| 12764 | var statusText = xhr.statusText || ''; |
| 12765 | |
| 12766 | // responseText is the old-school way of retrieving response (supported by IE9) |
| 12767 | // response/responseType properties were introduced in XHR Level2 spec (supported by IE10) |
| 12768 | var response = ('response' in xhr) ? xhr.response : xhr.responseText; |
| 12769 | |
| 12770 | // normalize IE9 bug (http://bugs.jquery.com/ticket/1450) |
| 12771 | var status = xhr.status === 1223 ? 204 : xhr.status; |
| 12772 | |
| 12773 | // fix status code when it is 0 (0 status is undocumented). |
| 12774 | // Occurs when accessing file resources or on Android 4.1 stock browser |
| 12775 | // while retrieving files from application cache. |
| 12776 | if (status === 0) { |
| 12777 | status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0; |
| 12778 | } |
| 12779 | |
| 12780 | completeRequest(callback, |
| 12781 | status, |
| 12782 | response, |
| 12783 | xhr.getAllResponseHeaders(), |
| 12784 | statusText, |
| 12785 | 'complete'); |
| 12786 | }; |
| 12787 | |
| 12788 | var requestError = function() { |
| 12789 | // The response is always empty |
| 12790 | // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error |
| 12791 | completeRequest(callback, -1, null, null, '', 'error'); |
| 12792 | }; |
| 12793 | |
| 12794 | var requestAborted = function() { |
| 12795 | completeRequest(callback, -1, null, null, '', 'abort'); |
| 12796 | }; |
no test coverage detected