Skip to content

Commit 3bae54a

Browse files
fras2560mgol
authored andcommitted
Ajax: Execute JSONP error script responses
Issue gh-4379 was meant to be a bug fix but the JSONP case is a bit special: under the hood it's a script but it simulates JSON responses in an environment without a CORS setup and sending JSON payloads on error responses is quite typical there. This commit makes JSONP error responses still execute the payload. The regular script error responses continue to be skipped. Fixes gh-4771 Closes gh-4773 (cherry picked from commit a1e619b)
1 parent beea433 commit 3bae54a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ajax.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,10 @@ jQuery.extend( {
745745
response = ajaxHandleResponses( s, jqXHR, responses );
746746
}
747747

748-
// Use a noop converter for missing script
749-
if ( !isSuccess && jQuery.inArray( "script", s.dataTypes ) > -1 ) {
748+
// Use a noop converter for missing script but not if jsonp
749+
if ( !isSuccess &&
750+
jQuery.inArray( "script", s.dataTypes ) > -1 &&
751+
jQuery.inArray( "json", s.dataTypes ) < 0 ) {
750752
s.converters[ "text script" ] = function() {};
751753
}
752754

test/unit/ajax.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,19 @@ QUnit.module( "ajax", {
837837
};
838838
} );
839839

840+
ajaxTest( "jQuery.ajax() - do execute scripts if JSONP from unsuccessful responses", 1, function( assert ) {
841+
var testMsg = "Unsuccessful JSONP requests should have a JSON body";
842+
return {
843+
dataType: "jsonp",
844+
url: url( "mock.php?action=errorWithScript" ),
845+
// error is the significant assertion
846+
error: function( xhr ) {
847+
var expected = { "status": 404, "msg": "Not Found" };
848+
assert.deepEqual( xhr.responseJSON, expected, testMsg );
849+
}
850+
};
851+
} );
852+
840853
ajaxTest( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)", 11, function( assert ) {
841854
var globalEval = jQuery.globalEval;
842855

0 commit comments

Comments
 (0)