Skip to content

Commit

Permalink
Fixes 4825. jQuery.fn.load: use the jXHR's Promise interface to get t…
Browse files Browse the repository at this point in the history
…he actual response in case ajaxSettings contains a dataFilter. Unit test added.
  • Loading branch information
jaubourg committed Jan 11, 2011
1 parent c5c1f18 commit 5a721cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/ajax.js
Expand Up @@ -61,26 +61,34 @@ jQuery.fn.extend({
type: type, type: type,
dataType: "html", dataType: "html",
data: params, data: params,
complete: function( res, status ) { // Complete callback (responseText is used internally)
complete: function( jXHR, status, responseText ) {
// Store the response as specified by the jXHR object
responseText = jXHR.responseText;
// If successful, inject the HTML into all the matched elements // If successful, inject the HTML into all the matched elements
if ( status === "success" || status === "notmodified" ) { if ( jXHR.isResolved() ) {
// #4825: Get the actual response in case
// a dataFilter is present in ajaxSettings
jXHR.done(function( r ) {
responseText = r;
});
// See if a selector was specified // See if a selector was specified
self.html( selector ? self.html( selector ?
// Create a dummy div to hold the results // Create a dummy div to hold the results
jQuery("<div>") jQuery("<div>")
// inject the contents of the document in, removing the scripts // inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE // to avoid any 'Permission Denied' errors in IE
.append(res.responseText.replace(rscript, "")) .append(responseText.replace(rscript, ""))


// Locate the specified elements // Locate the specified elements
.find(selector) : .find(selector) :


// If not, just inject the full result // If not, just inject the full result
res.responseText ); responseText );
} }


if ( callback ) { if ( callback ) {
self.each( callback, [res.responseText, status, res] ); self.each( callback, [responseText, status, jXHR] );
} }
} }
}); });
Expand Down
12 changes: 12 additions & 0 deletions test/unit/ajax.js
Expand Up @@ -1025,6 +1025,18 @@ test("load(String, Function) - check file with only a script tag", function() {
}); });
}); });


test("load(String, Function) - dataFilter in ajaxSettings", function() {
expect(2);
stop();
jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
jQuery.ajaxSetup({ dataFilter: 0 });
start();
});
});

test("load(String, Object, Function)", function() { test("load(String, Object, Function)", function() {
expect(2); expect(2);
stop(); stop();
Expand Down

0 comments on commit 5a721cf

Please sign in to comment.