Skip to content

Commit

Permalink
Ajax: Support an alternative completeCallback API for transports
Browse files Browse the repository at this point in the history
Apart from the existing API:

```js
function( status, statusText, responses, headers ) {}
```
a new API is now available:
```js
function( { status, statusText, responses, headers } ) {}
```

This makes it possible to add new parameters in the future without relying on
their order among parameters and being able to provide them selectively.

Ref gh-4405
  • Loading branch information
mgol committed Mar 1, 2020
1 parent 721744a commit 62e7f2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,19 @@ jQuery.extend( {

// Callback for when everything is done
function done( status, nativeStatusText, responses, headers ) {
var isSuccess, success, error, response, modified,
statusText = nativeStatusText;
var isSuccess, success, error, response, modified, statusText;

if ( typeof status === "object" ) {

// The new, object-based API
nativeStatusText = status.statusText;
responses = status.responses;
headers = status.headers;

status = status.status;
}

statusText = nativeStatusText;

// Ignore repeat invocations
if ( completed ) {
Expand Down
20 changes: 10 additions & 10 deletions src/ajax/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ jQuery.ajaxTransport( function( options ) {
if ( type === "abort" ) {
xhr.abort();
} else if ( type === "error" ) {
complete(
complete( {

// File: protocol always yields status 0; see #8605, #14207
xhr.status,
xhr.statusText
);
status: xhr.status,
statusText: xhr.statusText
} );
} else {
complete(
xhrSuccessStatus[ xhr.status ] || xhr.status,
xhr.statusText,
complete( {
status: xhrSuccessStatus[ xhr.status ] || xhr.status,
statusText: xhr.statusText,

// For XHR2 non-text, let the caller handle it (gh-2498)
( xhr.responseType || "text" ) === "text" ?
responses: ( xhr.responseType || "text" ) === "text" ?
{ text: xhr.responseText } :
{ binary: xhr.response },
xhr.getAllResponseHeaders()
);
headers: xhr.getAllResponseHeaders()
} );
}
}
};
Expand Down

0 comments on commit 62e7f2c

Please sign in to comment.