8000 Ajax: Support `null` as success functions in `jQuery.get` by mgol · Pull Request #5139 · jquery/jquery · GitHub
[go: up one dir, main page]

Skip to content

Ajax: Support null as success functions in jQuery.get #5139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,9 @@ jQuery.extend( {
jQuery.each( [ "get", "post" ], function( _i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {

// Shift arguments if data argument was omitted
if ( typeof data === "function" ) {
// Shift arguments if data argument was omitted.
// Handle the null callback placeholder.
if ( typeof data === "function" || data === null ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gibson042 allowing undefined here would break the 4-param signature with the second parameter (data) being undefined which is e.g. used in the $.getScript definition. We could add more checks to make it work but I thought in that case it may be better to just allow null. null is not an allowed data value so we don’t have a conflict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur.

type = type || callback;
callback = data;
data = undefined;
Expand Down
1 change: 0 additions & 1 deletion src/ajax/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var xhrSuccessStatus = {
jQuery.ajaxTransport( function( options ) {
var callback;

// Cross domain only allowed if supported through XMLHttpRequest
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated but this comment has long been invalid… We only have support checks on 3.x-stable here.

return {
send: function( headers, complete ) {
var i,
Expand Down
8 changes: 4 additions & 4 deletions test/data/mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ protected function json( $req ) {
}

if ( isset( $req->query['array'] ) ) {
echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]';
echo '[{"name":"John","age":21},{"name":"Peter","age":25}]';
} else {
echo '{ "data": {"lang": "en", "length": 25} }';
echo '{"data":{"lang":"en","length":25}}';
}
}

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

Expand Down
20 changes: 20 additions & 0 deletions test/unit/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,26 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re
} );
} );

QUnit.test( "jQuery.get( String, null-ish, String ) - dataType with null callback (gh-4989)",
function( assert ) {
assert.expect( 2 );
var done = assert.async( 2 );

jQuery.get( url( "mock.php?action=json&header" ), null, "json" )
.then( function( json ) {
assert.deepEqual( json, { data: { lang: "en", length: 25 } },
"`dataType: \"json\"` applied with a `null` callback" );
done();
} );

jQuery.get( url( "mock.php?action=json&header" ), null, "text" )
.then( function( text ) {
assert.strictEqual( text, "{\"data\":{\"lang\":\"en\",\"length\":25}}",
"`dataType: \"text\"` applied with a `null` callback" );
done();
} );
} );

//----------- jQuery.getJSON()

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