1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
var XHR = window.XMLHttpRequest || function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
throw new Error("This browser does not support XMLHttpRequest.");
};
/**
* @ngdoc object
* @name angular.module.ng.$httpBackend
* @requires $browser
* @requires $window
* @requires $document
*
* @description
*/
function $HttpBackendProvider() {
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks,
$document[0].body, $window.location.href.replace(':', ''));
}];
}
function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locationProtocol) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout) {
$browser.$$incOutstandingRequestCount();
if (lowercase(method) == 'jsonp') {
var callbackId = '_' + (callbacks.counter++).toString(36);
callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
};
var script = $browser.addJs(url.replace('JSON_CALLBACK', callbackId), null, function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
} else {
completeRequest(callback, -2);
}
delete callbacks[callbackId];
body.removeChild(script);
});
} else {
var xhr = new XHR();
xhr.open(method, url, true);
forEach(headers, function(value, key) {
if (value) xhr.setRequestHeader(key, value);
});
var status;
xhr.send(post || '');
// IE6, IE7 bug - does sync when serving from cache
if (xhr.readyState == 4) {
$browserDefer(function() {
completeRequest(callback, status || xhr.status, xhr.responseText);
}, 0);
} else {
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
completeRequest(callback, status || xhr.status, xhr.responseText);
}
};
}
if (timeout > 0) {
$browserDefer(function() {
status = -1;
xhr.abort();
}, timeout);
}
return xhr;
}
function completeRequest(callback, status, response) {
// URL_MATCH is defined in src/service/location.js
var protocol = (url.match(URL_MATCH) || ['', locationProtocol])[1];
// fix status code for file protocol (it's always 0)
status = protocol == 'file' ? (response ? 200 : 404) : status;
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
status = status == 1223 ? 204 : status;
callback(status, response);
$browser.$$completeOutstandingRequest(noop);
}
};
}
|