jquery ajax: Closes #2567, additional setting for $.ajax called 'dataFilter'. It's an optional function that receives the ajax response, and returns the sanitized version.

This commit is contained in:
Ariel Flesler 2008-05-16 16:39:27 +00:00
parent 2c2a6253e3
commit afc2ebde14

View file

@ -336,7 +336,7 @@ jQuery.extend({
// Watch for, and catch, XML document parse errors // Watch for, and catch, XML document parse errors
try { try {
// process the data (runs the xml through httpData regardless of callback) // process the data (runs the xml through httpData regardless of callback)
data = jQuery.httpData( xhr, s.dataType ); data = jQuery.httpData( xhr, s.dataType, s.dataFilter );
} catch(e) { } catch(e) {
status = "parsererror"; status = "parsererror";
} }
@ -460,7 +460,7 @@ jQuery.extend({
return false; return false;
}, },
httpData: function( xhr, type ) { httpData: function( xhr, type, filter ) {
var ct = xhr.getResponseHeader("content-type"), var ct = xhr.getResponseHeader("content-type"),
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
data = xml ? xhr.responseXML : xhr.responseText; data = xml ? xhr.responseXML : xhr.responseText;
@ -468,6 +468,10 @@ jQuery.extend({
if ( xml && data.documentElement.tagName == "parsererror" ) if ( xml && data.documentElement.tagName == "parsererror" )
throw "parsererror"; throw "parsererror";
// Allow a pre-filtering function to sanitize the response
if( filter )
data = filter( data, type );
// If the type is "script", eval it in global context // If the type is "script", eval it in global context
if ( type == "script" ) if ( type == "script" )
jQuery.globalEval( data ); jQuery.globalEval( data );