Skip to content

Commit 3ced5ab

Browse files
committed
Ajax: improve content-type detection
Cherry-picked from 239169b Fixes gh-2584 Closes gh-2643
1 parent 2a83417 commit 3ced5ab

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

src/ajax.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ jQuery.extend( {
322322
},
323323

324324
contents: {
325-
xml: /xml/,
326-
html: /html/,
327-
json: /json/
325+
xml: /\bxml\b/,
326+
html: /\bhtml/,
327+
json: /\bjson\b/
328328
},
329329

330330
responseFields: {

src/ajax/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jQuery.ajaxSetup( {
1818
"application/ecmascript, application/x-ecmascript"
1919
},
2020
contents: {
21-
script: /(?:java|ecma)script/
21+
script: /\b(?:java|ecma)script\b/
2222
},
2323
converters: {
2424
"text script": function( text ) {

test/data/ajax/content-type.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
$type = $_REQUEST['content-type'];
3+
header("Content-type: $type");
4+
echo $_REQUEST['response']
5+
?>

test/unit/ajax.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,109 @@ QUnit.module( "ajax", {
16301630
}
16311631
);
16321632

1633-
// //----------- jQuery.ajaxPrefilter()
1633+
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
1634+
return {
1635+
url: url( "data/ajax/content-type.php" ),
1636+
data: {
1637+
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1638+
"response": "<test/>"
1639+
},
1640+
success: function( result ) {
1641+
assert.strictEqual(
1642+
typeof result,
1643+
"string",
1644+
"Should handle it as a string, not xml"
1645+
);
1646+
}
1647+
};
1648+
} );
1649+
1650+
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
1651+
return {
1652+
url: url( "data/ajax/content-type.php" ),
1653+
data: {
1654+
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1655+
"response": "<test/>"
1656+
},
1657+
success: function( result ) {
1658+
assert.strictEqual(
1659+
typeof result,
1660+
"string",
1661+
"Should handle it as a string, not xml"
1662+
);
1663+
}
1664+
};
1665+
} );
1666+
1667+
ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
1668+
return {
1669+
url: url( "data/ajax/content-type.php" ),
1670+
data: {
1671+
"content-type": "test/jsontest",
1672+
"response": JSON.stringify({test: "test"})
1673+
},
1674+
success: function( result ) {
1675+
assert.strictEqual(
1676+
typeof result,
1677+
"string",
1678+
"Should handle it as a string, not json"
1679+
);
1680+
}
1681+
};
1682+
} );
1683+
1684+
ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
1685+
return {
1686+
url: url( "data/ajax/content-type.php" ),
1687+
data: {
1688+
"content-type": "test/htmltest",
1689+
"response": "<p>test</p>"
1690+
},
1691+
success: function( result ) {
1692+
assert.strictEqual(
1693+
typeof result,
1694+
"string",
1695+
"Should handle it as a string, not html"
1696+
);
1697+
}
1698+
};
1699+
} );
1700+
1701+
ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
1702+
return {
1703+
url: url( "data/ajax/content-type.php" ),
1704+
data: {
1705+
"content-type": "test/testjavascript",
1706+
"response": "alert(1)"
1707+
},
1708+
success: function( result ) {
1709+
assert.strictEqual(
1710+
typeof result,
1711+
"string",
1712+
"Should handle it as a string, not javascript"
1713+
);
1714+
}
1715+
};
1716+
} );
1717+
1718+
ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
1719+
return {
1720+
url: url( "data/ajax/content-type.php" ),
1721+
data: {
1722+
"content-type": "test/testjavascript",
1723+
"response": "alert(1)"
1724+
},
1725+
success: function( result ) {
1726+
assert.strictEqual(
1727+
typeof result,
1728+
"string",
1729+
"Should handle it as a string, not ecmascript"
1730+
);
1731+
}
1732+
};
1733+
} );
1734+
1735+
//----------- jQuery.ajaxPrefilter()
16341736

16351737
ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
16361738
return {

0 commit comments

Comments
 (0)