Skip to content

Commit 239169b

Browse files
committed
Ajax: improve content-type detection
Fixes gh-2584 Closes gh-2643
1 parent cb087ce commit 239169b

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
@@ -1794,7 +1794,109 @@ QUnit.module( "ajax", {
17941794
}
17951795
);
17961796

1797-
// //----------- jQuery.ajaxPrefilter()
1797+
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
1798+
return {
1799+
url: url( "data/ajax/content-type.php" ),
1800+
data: {
1801+
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1802+
"response": "<test/>"
1803+
},
1804+
success: function( result ) {
1805+
assert.strictEqual(
1806+
typeof result,
1807+
"string",
1808+
"Should handle it as a string, not xml"
1809+
);
1810+
}
1811+
};
1812+
} );
1813+
1814+
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
1815+
return {
1816+
url: url( "data/ajax/content-type.php" ),
1817+
data: {
1818+
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1819+
"response": "<test/>"
1820+
},
1821+
success: function( result ) {
1822+
assert.strictEqual(
1823+
typeof result,
1824+
"string",
1825+
"Should handle it as a string, not xml"
1826+
);
1827+
}
1828+
};
1829+
} );
1830+
1831+
ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
1832+
return {
1833+
url: url( "data/ajax/content-type.php" ),
1834+
data: {
1835+
"content-type": "test/jsontest",
1836+
"response": JSON.stringify({test: "test"})
1837+
},
1838+
success: function( result ) {
1839+
assert.strictEqual(
1840+
typeof result,
1841+
"string",
1842+
"Should handle it as a string, not json"
1843+
);
1844+
}
1845+
};
1846+
} );
1847+
1848+
ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
1849+
return {
1850+
url: url( "data/ajax/content-type.php" ),
1851+
data: {
1852+
"content-type": "test/htmltest",
1853+
"response": "<p>test</p>"
1854+
},
1855+
success: function( result ) {
1856+
assert.strictEqual(
1857+
typeof result,
1858+
"string",
1859+
"Should handle it as a string, not html"
1860+
);
1861+
}
1862+
};
1863+
} );
1864+
1865+
ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
1866+
return {
1867+
url: url( "data/ajax/content-type.php" ),
1868+
data: {
1869+
"content-type": "test/testjavascript",
1870+
"response": "alert(1)"
1871+
},
1872+
success: function( result ) {
1873+
assert.strictEqual(
1874+
typeof result,
1875+
"string",
1876+
"Should handle it as a string, not javascript"
1877+
);
1878+
}
1879+
};
1880+
} );
1881+
1882+
ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
1883+
return {
1884+
url: url( "data/ajax/content-type.php" ),
1885+
data: {
1886+
"content-type": "test/testjavascript",
1887+
"response": "alert(1)"
1888+
},
1889+
success: function( result ) {
1890+
assert.strictEqual(
1891+
typeof result,
1892+
"string",
1893+
"Should handle it as a string, not ecmascript"
1894+
);
1895+
}
1896+
};
1897+
} );
1898+
1899+
//----------- jQuery.ajaxPrefilter()
17981900

17991901
ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
18001902
return {

0 commit comments

Comments
 (0)