Skip to content

Commit 6861b5d

Browse files
committed
Added in support for content-type sniffing for scripts. Fixes #5718.
1 parent 787f271 commit 6861b5d

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/ajax.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,8 @@ jQuery.extend({
555555
},
556556

557557
httpData: function( xhr, type, s ) {
558-
var ct = xhr.getResponseHeader("content-type"),
559-
xml = type === "xml" || !type && ct && ct.indexOf("xml") >= 0,
560-
json = type === "json" || !type && ct && ct.indexOf("json") >= 0,
558+
var ct = xhr.getResponseHeader("content-type") || "",
559+
xml = type === "xml" || !type && ct.indexOf("xml") >= 0,
561560
data = xml ? xhr.responseXML : xhr.responseText;
562561

563562
if ( xml && data.documentElement.nodeName === "parsererror" ) {
@@ -572,14 +571,13 @@ jQuery.extend({
572571

573572
// The filter can actually parse the response
574573
if ( typeof data === "string" ) {
575-
576574
// If the type is "script", eval it in global context
577-
if ( type === "script" ) {
575+
if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
578576
jQuery.globalEval( data );
579577
}
580578

581579
// Get the JavaScript object, if JSON is used.
582-
if ( json ) {
580+
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
583581
// Try to use the native JSON parser first
584582
try {
585583
data = JSON.parse( data );

test/data/script.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
error_reporting(0);
3+
if ( $_REQUEST['header'] ) {
4+
header("Content-type: text/javascript");
5+
}
6+
?>
7+
ok( true, "Script executed correctly." );

test/unit/ajax.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,20 @@ test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
797797
});
798798
});
799799

800+
test("jQuery.ajax() - script by content-type", function() {
801+
expect(1);
802+
803+
stop();
804+
805+
jQuery.ajax({
806+
url: "data/script.php",
807+
data: { header: "script" },
808+
success: function() {
809+
start();
810+
}
811+
});
812+
});
813+
800814
test("jQuery.ajax() - json by content-type", function() {
801815
expect(5);
802816

0 commit comments

Comments
 (0)