($browser, createXhr, $browserDefer, callbacks, rawDocument)
| 11357 | } |
| 11358 | |
| 11359 | function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) { |
| 11360 | // TODO(vojta): fix the signature |
| 11361 | return function(method, url, post, callback, headers, timeout, withCredentials, responseType) { |
| 11362 | $browser.$$incOutstandingRequestCount(); |
| 11363 | url = url || $browser.url(); |
| 11364 | |
| 11365 | if (lowercase(method) == 'jsonp') { |
| 11366 | var callbackId = '_' + (callbacks.counter++).toString(36); |
| 11367 | callbacks[callbackId] = function(data) { |
| 11368 | callbacks[callbackId].data = data; |
| 11369 | callbacks[callbackId].called = true; |
| 11370 | }; |
| 11371 | |
| 11372 | var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), |
| 11373 | callbackId, function(status, text) { |
| 11374 | completeRequest(callback, status, callbacks[callbackId].data, "", text); |
| 11375 | callbacks[callbackId] = noop; |
| 11376 | }); |
| 11377 | } else { |
| 11378 | |
| 11379 | var xhr = createXhr(method, url); |
| 11380 | |
| 11381 | xhr.open(method, url, true); |
| 11382 | forEach(headers, function(value, key) { |
| 11383 | if (isDefined(value)) { |
| 11384 | xhr.setRequestHeader(key, value); |
| 11385 | } |
| 11386 | }); |
| 11387 | |
| 11388 | xhr.onload = function requestLoaded() { |
| 11389 | var statusText = xhr.statusText || ''; |
| 11390 | |
| 11391 | // responseText is the old-school way of retrieving response (supported by IE9) |
| 11392 | // response/responseType properties were introduced in XHR Level2 spec (supported by IE10) |
| 11393 | var response = ('response' in xhr) ? xhr.response : xhr.responseText; |
| 11394 | |
| 11395 | // normalize IE9 bug (http://bugs.jquery.com/ticket/1450) |
| 11396 | var status = xhr.status === 1223 ? 204 : xhr.status; |
| 11397 | |
| 11398 | // fix status code when it is 0 (0 status is undocumented). |
| 11399 | // Occurs when accessing file resources or on Android 4.1 stock browser |
| 11400 | // while retrieving files from application cache. |
| 11401 | if (status === 0) { |
| 11402 | status = response ? 200 : urlResolve(url).protocol == 'file' ? 404 : 0; |
| 11403 | } |
| 11404 | |
| 11405 | completeRequest(callback, |
| 11406 | status, |
| 11407 | response, |
| 11408 | xhr.getAllResponseHeaders(), |
| 11409 | statusText); |
| 11410 | }; |
| 11411 | |
| 11412 | var requestError = function() { |
| 11413 | // The response is always empty |
| 11414 | // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error |
| 11415 | completeRequest(callback, -1, null, null, ''); |
| 11416 | }; |
no test coverage detected