Skip to content

Commit 209560e

Browse files
committed
Ajax: simplify XHR detection.
Closes jquery#1967
1 parent ab20d9d commit 209560e

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

src/ajax/xhr.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
define([
22
"../core",
3-
"../var/support",
4-
"../ajax"
3+
"../var/support"
54
], function( jQuery, support ) {
65

76
jQuery.ajaxSettings.xhr = function() {
8-
try {
9-
return new XMLHttpRequest();
10-
} catch ( e ) {}
7+
return new XMLHttpRequest();
118
};
129

1310
var xhrId = 0,
@@ -18,8 +15,7 @@ var xhrId = 0,
1815
// Support: IE9
1916
// #1450: sometimes IE returns 1223 when it should be 204
2017
1223: 204
21-
},
22-
xhrSupported = jQuery.ajaxSettings.xhr();
18+
};
2319

2420
// Support: IE9
2521
// Open requests must be manually aborted on unload (#5280)
@@ -32,14 +28,14 @@ if ( window.attachEvent ) {
3228
});
3329
}
3430

35-
support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
36-
support.ajax = xhrSupported = !!xhrSupported;
31+
// Support: IE9
32+
support.cors = "withCredentials" in jQuery.ajaxSettings.xhr();
3733

3834
jQuery.ajaxTransport(function( options ) {
3935
var callback;
4036

4137
// Cross domain only allowed if supported through XMLHttpRequest
42-
if ( support.cors || xhrSupported && !options.crossDomain ) {
38+
if ( support.cors || !options.crossDomain ) {
4339
return {
4440
send: function( headers, complete ) {
4541
var i,

test/unit/support.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
5555
// Catches Chrome on Android as well (i.e. the default
5656
// Android browser on Android >= 4.4).
5757
expected = {
58-
"ajax": true,
5958
"boxSizingReliable": true,
6059
"checkClone": true,
6160
"checkOn": true,
@@ -72,7 +71,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
7271
};
7372
} else if ( /(msie 10\.0|trident\/7\.0)/i.test( userAgent ) ) {
7473
expected = {
75-
"ajax": true,
7674
"boxSizingReliable": false,
7775
"checkClone": true,
7876
"checkOn": true,
@@ -89,7 +87,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
8987
};
9088
} else if ( /msie 9\.0/i.test( userAgent ) ) {
9189
expected = {
92-
"ajax": true,
9390
"boxSizingReliable": false,
9491
"checkClone": true,
9592
"checkOn": true,
@@ -106,7 +103,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
106103
};
107104
} else if ( /8.0(\.\d+|) safari/i.test( userAgent ) ) {
108105
expected = {
109-
"ajax": true,
110106
"boxSizingReliable": true,
111107
"checkClone": true,
112108
"checkOn": true,
@@ -123,7 +119,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
123119
};
124120
} else if ( /(6|7)\.0(\.\d+|) safari/i.test( userAgent ) ) {
125121
expected = {
126-
"ajax": true,
127122
"boxSizingReliable": true,
128123
"checkClone": true,
129124
"checkOn": true,
@@ -140,7 +135,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
140135
};
141136
} else if ( /firefox/i.test( userAgent ) ) {
142137
expected = {
143-
"ajax": true,
144138
"boxSizingReliable": true,
145139
"checkClone": true,
146140
"checkOn": true,
@@ -157,7 +151,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
157151
};
158152
} else if ( /iphone os 8/i.test( userAgent ) ) {
159153
expected = {
160-
"ajax": true,
161154
"boxSizingReliable": true,
162155
"checkClone": true,
163156
"checkOn": true,
@@ -174,7 +167,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
174167
};
175168
} else if ( /iphone os (6|7)/i.test( userAgent ) ) {
176169
expected = {
177-
"ajax": true,
178170
"boxSizingReliable": true,
179171
"checkClone": true,
180172
"checkOn": true,
@@ -191,7 +183,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
191183
};
192184
} else if ( /android 4\.[0-3]/i.test( userAgent ) ) {
193185
expected = {
194-
"ajax": true,
195186
"boxSizingReliable": true,
196187
"checkClone": false,
197188
"checkOn": false,
@@ -208,7 +199,6 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
208199
};
209200
} else if ( /android 2\.3/i.test( userAgent ) ) {
210201
expected = {
211-
"ajax": true,
212202
"boxSizingReliable": true,
213203
"checkClone": true,
214204
"checkOn": false,
@@ -238,7 +228,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
238228

239229
for ( i in expected ) {
240230
// TODO check for all modules containing support properties
241-
if ( jQuery.ajax || i !== "ajax" && i !== "cors" ) {
231+
if ( jQuery.ajax || i !== "cors" ) {
242232
equal( computedSupport[ i ], expected[ i ],
243233
"jQuery.support['" + i + "']: " + computedSupport[ i ] +
244234
", expected['" + i + "']: " + expected[ i ]);

0 commit comments

Comments
 (0)