mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Ajax: improve content-type detection
Cherry-picked from 239169bb2e
Fixes gh-2584
Closes gh-2643
This commit is contained in:
parent
2a8341787a
commit
3ced5abe5c
@ -322,9 +322,9 @@ jQuery.extend( {
|
|||||||
},
|
},
|
||||||
|
|
||||||
contents: {
|
contents: {
|
||||||
xml: /xml/,
|
xml: /\bxml\b/,
|
||||||
html: /html/,
|
html: /\bhtml/,
|
||||||
json: /json/
|
json: /\bjson\b/
|
||||||
},
|
},
|
||||||
|
|
||||||
responseFields: {
|
responseFields: {
|
||||||
|
@ -18,7 +18,7 @@ jQuery.ajaxSetup( {
|
|||||||
"application/ecmascript, application/x-ecmascript"
|
"application/ecmascript, application/x-ecmascript"
|
||||||
},
|
},
|
||||||
contents: {
|
contents: {
|
||||||
script: /(?:java|ecma)script/
|
script: /\b(?:java|ecma)script\b/
|
||||||
},
|
},
|
||||||
converters: {
|
converters: {
|
||||||
"text script": function( text ) {
|
"text script": function( text ) {
|
||||||
|
5
test/data/ajax/content-type.php
Normal file
5
test/data/ajax/content-type.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
$type = $_REQUEST['content-type'];
|
||||||
|
header("Content-type: $type");
|
||||||
|
echo $_REQUEST['response']
|
||||||
|
?>
|
@ -1630,7 +1630,109 @@ QUnit.module( "ajax", {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// //----------- jQuery.ajaxPrefilter()
|
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
"response": "<test/>"
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not xml"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
"response": "<test/>"
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not xml"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "test/jsontest",
|
||||||
|
"response": JSON.stringify({test: "test"})
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not json"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "test/htmltest",
|
||||||
|
"response": "<p>test</p>"
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not html"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "test/testjavascript",
|
||||||
|
"response": "alert(1)"
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not javascript"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
|
||||||
|
return {
|
||||||
|
url: url( "data/ajax/content-type.php" ),
|
||||||
|
data: {
|
||||||
|
"content-type": "test/testjavascript",
|
||||||
|
"response": "alert(1)"
|
||||||
|
},
|
||||||
|
success: function( result ) {
|
||||||
|
assert.strictEqual(
|
||||||
|
typeof result,
|
||||||
|
"string",
|
||||||
|
"Should handle it as a string, not ecmascript"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} );
|
||||||
|
|
||||||
|
//----------- jQuery.ajaxPrefilter()
|
||||||
|
|
||||||
ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
|
ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user