Skip to content

Commit 74978b7

Browse files
authored
Ajax: Support null as success functions in jQuery.get
According to the docs, one can use `null` as a success function in `jQuery.get` of `jQuery.post` so the following: ```js await jQuery.get( "https://httpbin.org/json", null, "text" ) ``` should get the text result. However, this shortcut hasn't been working so far. Fixes gh-4989 Closes gh-5139
1 parent 8c7da22 commit 74978b7

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/ajax.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,9 @@ jQuery.extend( {
846846
jQuery.each( [ "get", "post" ], function( _i, method ) {
847847
jQuery[ method ] = function( url, data, callback, type ) {
848848

849-
// Shift arguments if data argument was omitted
850-
if ( typeof data === "function" ) {
849+
// Shift arguments if data argument was omitted.
850+
// Handle the null callback placeholder.
851+
if ( typeof data === "function" || data === null ) {
851852
type = type || callback;
852853
callback = data;
853854
data = undefined;

src/ajax/xhr.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var xhrSuccessStatus = {
1515
jQuery.ajaxTransport( function( options ) {
1616
var callback;
1717

18-
// Cross domain only allowed if supported through XMLHttpRequest
1918
return {
2019
send: function( headers, complete ) {
2120
var i,

test/data/mock.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ protected function json( $req ) {
9595
}
9696

9797
if ( isset( $req->query['array'] ) ) {
98-
echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]';
98+
echo '[{"name":"John","age":21},{"name":"Peter","age":25}]';
9999
} else {
100-
echo '{ "data": {"lang": "en", "length": 25} }';
100+
echo '{"data":{"lang":"en","length":25}}';
101101
}
102102
}
103103

@@ -112,8 +112,8 @@ protected function jsonp( $req ) {
112112
$callback = $_POST['callback'];
113113
}
114114
$json = isset( $req->query['array'] ) ?
115-
'[ { "name": "John", "age": 21 }, { "name": "Peter", "age": 25 } ]' :
116-
'{ "data": { "lang": "en", "length": 25 } }';
115+
'[{"name":"John","age":21},{"name":"Peter","age":25}]' :
116+
'{"data":{"lang":"en","length":25}}';
117117
echo cleanCallback( $callback ) . '(' . $json . ')';
118118
}
119119

test/unit/ajax.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,26 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re
25932593
} );
25942594
} );
25952595

2596+
QUnit.test( "jQuery.get( String, null-ish, String ) - dataType with null callback (gh-4989)",
2597+
function( assert ) {
2598+
assert.expect( 2 );
2599+
var done = assert.async( 2 );
2600+
2601+
jQuery.get( url( "mock.php?action=json&header" ), null, "json" )
2602+
.then( function( json ) {
2603+
assert.deepEqual( json, { data: { lang: "en", length: 25 } },
2604+
"`dataType: \"json\"` applied with a `null` callback" );
2605+
done();
2606+
} );
2607+
2608+
jQuery.get( url( "mock.php?action=json&header" ), null, "text" )
2609+
.then( function( text ) {
2610+
assert.strictEqual( text, "{\"data\":{\"lang\":\"en\",\"length\":25}}",
2611+
"`dataType: \"text\"` applied with a `null` callback" );
2612+
done();
2613+
} );
2614+
} );
2615+
25962616
//----------- jQuery.getJSON()
25972617

25982618
QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) {

0 commit comments

Comments
 (0)