Skip to content

Commit 61f812b

Browse files
committed
Ajax: Use the native XHR for all non-local requests in IE9+
IE throws an error on cross-domain PATCH requests if issued via the ActiveX interface. This commit switches the logic to use the native XHR in all non-local requests. Fixes gh-1684 Closes gh-2183
1 parent 3699ef4 commit 61f812b

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

src/ajax/xhr.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,31 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject !== undefined ?
1111
function() {
1212

1313
// XHR cannot access local files, always use ActiveX for that case
14-
return !this.isLocal &&
14+
if ( this.isLocal ) {
15+
return createActiveXHR();
16+
}
17+
18+
// Support: IE 10-11
19+
// IE seems to error on cross-domain PATCH requests when ActiveX XHR
20+
// is used. In IE 9+ always use the native XHR.
21+
// Note: this condition won't catch Spartan as it doesn't define
22+
// document.documentMode but it also doesn't have ActiveX so it won't
23+
// reach this code.
24+
if ( document.documentMode > 8 ) {
25+
return createStandardXHR();
26+
}
1527

16-
// Support: IE<9
17-
// oldIE XHR does not support non-RFC2616 methods (#13240)
18-
// See http://msdn.microsoft.com/en-us/library/ie/ms536648(v=vs.85).aspx
19-
// and http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9
20-
// Although this check for six methods instead of eight
21-
// since IE also does not support "trace" and "connect"
22-
/^(get|post|head|put|delete|options)$/i.test( this.type ) &&
28+
// Support: IE<9
29+
// oldIE XHR does not support non-RFC2616 methods (#13240)
30+
// See http://msdn.microsoft.com/en-us/library/ie/ms536648(v=vs.85).aspx
31+
// and http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9
32+
// Although this check for six methods instead of eight
33+
// since IE also does not support "trace" and "connect"
34+
if ( /^(get|post|head|put|delete|options)$/i.test( this.type ) ) {
35+
return createActiveXHR();
36+
}
2337

24-
createStandardXHR() || createActiveXHR();
38+
return createStandardXHR();
2539
} :
2640
// For all other browsers, use the standard XMLHttpRequest object
2741
createStandardXHR;

test/integration/gh-1684-ajax.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Test for gh-1684</title>
7+
<style>
8+
#result {
9+
font-size: 24px;
10+
margin: 0.5em 0;
11+
}
12+
#response {
13+
white-space: pre;
14+
}
15+
.error {
16+
background-color: red;
17+
}
18+
.warn {
19+
background-color: yellow;
20+
}
21+
.success {
22+
background-color: lightgreen;
23+
}
24+
</style>
25+
</head>
26+
27+
<body>
28+
<div id="result"></div>
29+
<div id="response"></div>
30+
<script src="../../dist/jquery.js"></script>
31+
<script>
32+
if ( !jQuery.support.cors ) {
33+
jQuery( "#result" )
34+
.addClass( "success" )
35+
.text( "CORS not supported in this browser. Test not run." );
36+
} else {
37+
jQuery.ajax( {
38+
url: "http://httpbin.org/patch",
39+
method: "PATCH",
40+
success: function( data ) {
41+
jQuery( "#result" ).addClass( "success" ).text( "Test passed." );
42+
jQuery( "#response" ).text( "Response:\n" + JSON.stringify( data, null, 4 ) );
43+
},
44+
error: function( error ) {
45+
jQuery( "#result" ).addClass( "error" ).text( "Test failed." );
46+
jQuery( "#response" ).text( "Error:\n" + JSON.stringify( error, null, 4 ) );
47+
}
48+
} );
49+
}
50+
</script>
51+
</body>
52+
</html>

test/unit/ajax.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,10 @@ module( "ajax", {
15781578
}
15791579
} );
15801580

1581-
// BrowserStack PATCH support sometimes breaks so on TestSwarm run the test in IE8 only.
1582-
if ( location.search.indexOf( "swarmURL=" ) === -1 || document.documentMode < 9 ) {
1581+
// BrowserStack PATCH support sometimes breaks so on TestSwarm run the test in IE only.
1582+
// Unfortunately, all IE versions gets special treatment in request object creation
1583+
// so we need to test in all supported IE versions to be sure.
1584+
if ( location.search.indexOf( "swarmURL=" ) === -1 || document.documentMode ) {
15831585
ajaxTest( "#13240 - jQuery.ajax() - support non-RFC2616 methods", 1, {
15841586
url: "data/echoQuery.php",
15851587
method: "PATCH",

0 commit comments

Comments
 (0)