Skip to content

Commit a767826

Browse files
John ResigJohn Resig
John Resig
authored and
John Resig
committed
Disable the X-Requested-With header to avoid preflighting remote POST requests. Fixes #4601.
1 parent 0f6e9a8 commit a767826

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/ajax.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,12 @@ jQuery.extend({
275275
}
276276

277277
// Matches an absolute URL, and saves the domain
278-
var parts = rurl.exec( s.url );
278+
var parts = rurl.exec( s.url ),
279+
remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
279280

280281
// If we're requesting a remote document
281282
// and trying to load JSON or Script with a GET
282-
if ( s.dataType === "script" && type === "GET" && parts
283-
&& ( parts[1] && parts[1] !== location.protocol || parts[2] !== location.host )) {
284-
283+
if ( s.dataType === "script" && type === "GET" && remote ) {
285284
var head = document.getElementsByTagName("head")[0] || document.documentElement;
286285
var script = document.createElement("script");
287286
script.src = s.url;
@@ -350,7 +349,10 @@ jQuery.extend({
350349
}
351350

352351
// Set header so the called script knows that it's an XMLHttpRequest
353-
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
352+
// Only send the header if it's not a remote XHR
353+
if ( !remote ) {
354+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
355+
}
354356

355357
// Set the Accepts header for the server, depending on the dataType
356358
xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?

0 commit comments

Comments
 (0)