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
This commit is contained in:
Michał Gołębiowski-Owczarek 2022-10-17 18:54:28 +02:00 committed by GitHub
parent 8c7da22cae
commit 74978b7e89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 7 deletions

View File

@ -846,8 +846,9 @@ jQuery.extend( {
jQuery.each( [ "get", "post" ], function( _i, method ) { jQuery.each( [ "get", "post" ], function( _i, method ) {
jQuery[ method ] = function( url, data, callback, type ) { jQuery[ method ] = function( url, data, callback, type ) {
// Shift arguments if data argument was omitted // Shift arguments if data argument was omitted.
if ( typeof data === "function" ) { // Handle the null callback placeholder.
if ( typeof data === "function" || data === null ) {
type = type || callback; type = type || callback;
callback = data; callback = data;
data = undefined; data = undefined;

View File

@ -15,7 +15,6 @@ var xhrSuccessStatus = {
jQuery.ajaxTransport( function( options ) { jQuery.ajaxTransport( function( options ) {
var callback; var callback;
// Cross domain only allowed if supported through XMLHttpRequest
return { return {
send: function( headers, complete ) { send: function( headers, complete ) {
var i, var i,

View File

@ -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() //----------- jQuery.getJSON()
QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) { QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) {