8000 Ajax: Support `null` & `undefined` as success functions in `jQuery.get` · jquery/jquery@d75d41e · GitHub
[go: up one dir, main page]

Skip to content

Commit d75d41e

Browse files
committed
Ajax: Support null & undefined as success functions in jQuery.get
According to the docs, one can use `null` as a success function in `jQuery.get` 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
1 parent 8c7da22 commit d75d41e

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/ajax.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ jQuery.each( [ "get", "post" ], function( _i, method ) {
847847
jQuery[ method ] = function( url, data, callback, type ) {
848848

849849
// Shift arguments if data argument was omitted
850-
if ( typeof data === "function" ) {
850+
if ( typeof data === "function" || data == null ) {
851851
type = type || callback;
852852
callback = data;
853853
data = undefined;

test/data/mock.php

+4-4
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

+34
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,40 @@ 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( 4 );
2599+
var done = assert.async( 4 );
2600+
2601+
jQuery.get( url( "mock.php?action=json&header" ), undefined, "json" )
2602+
.then( function( json ) {
2603+
assert.deepEqual( json, { data: { lang: "en", length: 25 } },
2604+
"`dataType: \"json\"` applied with an `undefined` callback" );
2605+
done();
2606+
} );
2607+
2608+
jQuery.get( url( "mock.php?action=json&header" ), null, "json" )
2609+
.then( function( json ) {
2610+
assert.deepEqual( json, { data: { lang: "en", length: 25 } },
2611+
"`dataType: \"json\"` applied with a `null` callback" );
2612+
done();
2613+
} );
2614+
2615+
jQuery.get( url( "mock.php?action=json&header" ), undefined, "text" )
2616+
.then( function( text ) {
2617+
assert.strictEqual( text, "{\"data\":{\"lang\":\"en\",\"length\":25}}",
2618+
"`dataType: \"text\"` applied with an `undefined` callback" );
2619+
done();
2620+
} );
2621+
2622+
jQuery.get( url( "mock.php?action=json&header" ), null, "text" )
2623+
.then( function( text ) {
2624+
assert.strictEqual( text, "{\"data\":{\"lang\":\"en\",\"length\":25}}",
2625+
"`dataType: \"text\"` applied with a `null` callback" );
2626+
done();
2627+
} );
2628+
} );
2629+
25962630
//----------- jQuery.getJSON()
25972631

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

0 commit comments

Comments
 (0)
0