@@ -36,16 +36,41 @@ define(['module'], function (module) {
36
36
return external ;
37
37
}
38
38
39
+ /**
40
+ * Checks that url match current location
41
+ *
42
+ * @param {String } url
43
+ * @returns {Boolean }
44
+ */
45
+ function sameDomain ( url ) {
46
+ var uProtocol , uHostName , uPort ,
47
+ xdRegExp = / ^ ( [ \w : ] + ) ? \/ \/ ( [ ^ \/ \\ ] + ) / i,
48
+ location = window . location ,
49
+ match = xdRegExp . exec ( url ) ;
50
+
51
+ if ( ! match ) {
52
+ return true ;
53
+ }
54
+ uProtocol = match [ 1 ] ;
55
+ uHostName = match [ 2 ] ;
56
+
57
+ uHostName = uHostName . split ( ':' ) ;
58
+ uPort = uHostName [ 1 ] || '' ;
59
+ uHostName = uHostName [ 0 ] ;
60
+
61
+ return ( ! uProtocol || uProtocol === location . protocol ) &&
62
+ ( ! uHostName || uHostName . toLowerCase ( ) === location . hostname . toLowerCase ( ) ) &&
63
+ ( ( ! uPort && ! uHostName ) || uPort === location . port ) ;
64
+ }
65
+
39
66
/**
40
67
* @returns {XMLHttpRequest|XDomainRequest|null }
41
68
*/
42
- function createRequest ( ) {
43
- var xhr = null ;
69
+ function createRequest ( url ) {
70
+ var xhr = new XMLHttpRequest ( ) ;
44
71
45
- if ( typeof XDomainRequest !== 'undefined' ) {
72
+ if ( ! sameDomain ( url ) && typeof XDomainRequest !== 'undefined' ) {
46
73
xhr = new XDomainRequest ( ) ;
47
- } else if ( typeof XMLHttpRequest !== 'undefined' ) {
48
- xhr = new XMLHttpRequest ( ) ;
49
74
}
50
75
51
76
return xhr ;
@@ -60,56 +85,46 @@ define(['module'], function (module) {
60
85
* @param {Object } headers
61
86
*/
62
87
function getContent ( url , callback , fail , headers ) {
63
- var xhr = createRequest ( ) ,
88
+ var xhr = createRequest ( url ) ,
64
89
header ,
65
90
errorHandler = fail || Function ( ) ;
66
91
67
- xhr . open ( 'GET' , url , true ) ;
68
-
69
- //Allow plugins direct access to xhr headers
70
- for ( header in headers ) {
71
- if ( headers . hasOwnProperty ( header ) ) {
72
- xhr . setRequestHeader ( header . toLowerCase ( ) , headers [ header ] ) ;
92
+ if ( 'setRequestHeader' in xhr && headers ) {
93
+ for ( header in headers ) {
94
+ if ( headers . hasOwnProperty ( header ) ) {
95
+ xhr . setRequestHeader ( header . toLowerCase ( ) , headers [ header ] ) ;
96
+ }
73
97
}
74
98
}
75
99
76
- //Allow overrides specified in config
77
100
if ( defaultConfig . onXhr ) {
78
101
defaultConfig . onXhr ( xhr , url ) ;
79
102
}
80
103
81
104
/**
82
105
* onload handler
83
106
*/
84
- xhr . onreadystatechange = function ( ) {
85
- var status = xhr . status || 0 ,
86
- error ;
87
-
88
- if ( xhr . readyState !== 4 ) {
89
- return ;
90
- }
91
-
92
- if ( status > 399 && status < 600 ) {
93
- //An http 4xx or 5xx error. Signal an error.
94
- error = new Error ( url + ' HTTP status: ' + status ) ;
95
- error . xhr = xhr ;
96
-
97
- errorHandler ( error ) ;
98
-
99
- if ( defaultConfig . onXhrFailure ) {
100
- defaultConfig . onXhrFailure ( xhr , url ) ;
101
- }
102
-
103
- return ;
104
- }
107
+ xhr . onload = function ( ) {
105
108
106
109
callback ( xhr . responseText ) ;
107
110
108
111
if ( defaultConfig . onXhrComplete ) {
109
112
defaultConfig . onXhrComplete ( xhr , url ) ;
110
113
}
111
114
} ;
112
- xhr . send ( null ) ;
115
+ /**
116
+ * onerror handler
117
+ */
118
+ xhr . onerror = function ( event ) {
119
+ errorHandler ( event ) ;
120
+
121
+ if ( defaultConfig . onXhrFailure ) {
122
+ defaultConfig . onXhrFailure ( xhr , url , event ) ;
123
+ }
124
+ } ;
125
+
126
+ xhr . open ( 'GET' , url ) ;
127
+ xhr . send ( ) ;
113
128
}
114
129
115
130
/**
0 commit comments