@@ -6,7 +6,7 @@ var XHR = window.XMLHttpRequest || function() {
6
6
try { return new ActiveXObject ( "Msxml2.XMLHTTP.3.0" ) ; } catch ( e2 ) { }
7
7
try { return new ActiveXObject ( "Msxml2.XMLHTTP" ) ; } catch ( e3 ) { }
8
8
throw minErr ( '$httpBackend' ) ( 'noxhr' , "This browser does not support XMLHttpRequest." ) ;
9
- } ;
9
+ } , XDR = ! window . msPerformance && window . XDomainRequest || null ;
10
10
11
11
12
12
/**
@@ -28,14 +28,14 @@ var XHR = window.XMLHttpRequest || function() {
28
28
*/
29
29
function $HttpBackendProvider ( ) {
30
30
this . $get = [ '$browser' , '$window' , '$document' , function ( $browser , $window , $document ) {
31
- return createHttpBackend ( $browser , XHR , $browser . defer , $window . angular . callbacks ,
31
+ return createHttpBackend ( $browser , XHR , XDR , $browser . defer , $window . angular . callbacks ,
32
32
$document [ 0 ] , $window . location . protocol . replace ( ':' , '' ) ) ;
33
33
} ] ;
34
34
}
35
35
36
- function createHttpBackend ( $browser , XHR , $browserDefer , callbacks , rawDocument , locationProtocol ) {
36
+ function createHttpBackend ( $browser , XHR , XDR , $browserDefer , callbacks , rawDocument , locationProtocol ) {
37
37
// TODO(vojta): fix the signature
38
- return function ( method , url , post , callback , headers , timeout , withCredentials , responseType ) {
38
+ return function ( method , url , post , callback , headers , timeout , withCredentials , responseType , useXDomain ) {
39
39
var status ;
40
40
$browser . $$incOutstandingRequestCount ( ) ;
41
41
url = url || $browser . url ( ) ;
@@ -56,39 +56,75 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
56
56
delete callbacks [ callbackId ] ;
57
57
} ) ;
58
58
} else {
59
- var xhr = new XHR ( ) ;
60
- xhr . open ( method , url , true ) ;
61
- forEach ( headers , function ( value , key ) {
62
- if ( isDefined ( value ) ) {
63
- xhr . setRequestHeader ( key , value ) ;
59
+ var status ;
60
+ if ( useXDomain && XDR ) {
61
+ var xdr = new XDR ( ) ;
62
+ xdr . open ( method . toLowerCase ( ) , url ) ;
63
+
64
+ // Required to XDomainRequest works
65
+ xdr . timeout = timeout ;
66
+ xdr . onprogress = function ( ) { } ;
67
+
68
+ xdr . ontimeout = function ( ) {
69
+ completeRequest ( callback , 408 , 'Timeout' , 'Content-Type: text/plain' ) ;
70
+ xdr . abort ( ) ;
71
+ } ;
72
+
73
+ xdr . onload = function ( ) {
74
+ completeRequest ( callback , 200 , xdr . responseText , 'Content-Type: ' + xdr . contentType ) ;
75
+ } ;
76
+
77
+ xdr . onerror = function ( ) {
78
+ completeRequest ( callback , 500 , 'Error' , 'Content-Type: text/plain' ) ;
79
+ xdr . abort ( ) ;
80
+ } ;
81
+
82
+
83
+ $browserDefer ( function ( ) {
84
+ xdr . send ( ) ;
85
+ } , 0 ) ; //fix IE bug that raises '$apply already in progress' on cached requests
86
+
87
+ if ( timeout > 0 ) {
88
+ $browserDefer ( function ( ) {
89
+ status = - 1 ;
90
+ xdr . abort ( ) ;
91
+ } , timeout ) ;
64
92
}
65
- } ) ;
93
+ } else {
94
+ var xhr = new XHR ( ) ;
95
+ xhr . open ( method , url , true ) ;
96
+ forEach ( headers , function ( value , key ) {
97
+ if ( isDefined ( value ) ) {
98
+ xhr . setRequestHeader ( key , value ) ;
99
+ }
100
+ } ) ;
101
+
102
+ // In IE6 and 7, this might be called synchronously when xhr.send below is called and the
103
+ // response is in the cache. the promise api will ensure that to the app code the api is
104
+ // always async
105
+ xhr . onreadystatechange = function ( ) {
106
+ if ( xhr . readyState == 4 ) {
107
+ var responseHeaders = xhr . getAllResponseHeaders ( ) ;
108
+
109
+ // responseText is the old-school way of retrieving response (supported by IE8 & 9)
110
+ // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
111
+ completeRequest ( callback ,
112
+ status || xhr . status ,
113
+ ( xhr . responseType ? xhr . response : xhr . responseText ) ,
114
+ responseHeaders ) ;
115
+ }
116
+ } ;
66
117
67
- // In IE6 and 7, this might be called synchronously when xhr.send below is called and the
68
- // response is in the cache. the promise api will ensure that to the app code the api is
69
- // always async
70
- xhr . onreadystatechange = function ( ) {
71
- if ( xhr . readyState == 4 ) {
72
- var responseHeaders = xhr . getAllResponseHeaders ( ) ;
73
-
74
- // responseText is the old-school way of retrieving response (supported by IE8 & 9)
75
- // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
76
- completeRequest ( callback ,
77
- status || xhr . status ,
78
- ( xhr . responseType ? xhr . response : xhr . responseText ) ,
79
- responseHeaders ) ;
118
+ if ( withCredentials ) {
119
+ xhr . withCredentials = true ;
80
120
}
81
- } ;
82
121
83
- if ( withCredentials ) {
84
- xhr . withCredentials = true ;
85
- }
122
+ if ( responseType ) {
123
+ xhr . responseType = responseType ;
124
+ }
86
125
87
- if ( responseType ) {
88
- xhr . responseType = responseType ;
126
+ xhr . send ( post || null ) ;
89
127
}
90
-
91
- xhr . send ( post || null ) ;
92
128
}
93
129
94
130
if ( timeout > 0 ) {
0 commit comments