Respect content-type header in httpData()

Respect content-type header in httpData()


Currently, if you make a $.ajax() request and you're not sure whether
the response will be HTML or JSON the local onSuccess AJAX event (and
other local AJAX events) don't have a clue of the type of data they
have to manage.
If the httpData() function (the one internally used by jQuery core to
process the data in the response) respected the content-type header
(i.e. parsing the response as a JSON object if content-type is
"application/json") then the local AJAX events would know that the
response is JSON if its type is "object" and that it's HTML if its
type is "string".
My particular use-case is a generic wrapper for $.ajax() where the
response data is injected in the DOM if it's a string (it assumes that
it's HTML) and returned if it's an object (it assumes that it's JSON
data).
I think the change is very simple and quite useful so it would be a
good idea to apply it to the jQuery core.
The change would involve replacing the current httpData() function:
[code]
    httpData: function( xhr, type, filter ) {
        var ct = xhr.getResponseHeader("content-type"),
            xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
            data = xml ? xhr.responseXML : xhr.responseText;
        if ( xml && data.documentElement.tagName == "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 ( type == "script" )
            jQuery.globalEval( data );
        // Get the JavaScript object, if JSON is used.
        if ( type == "json" )
            data = eval("(" + data + ")");
        return data;
    },
[/code]
with this slightly modified version:
[code]
    httpData: function( xhr, type, filter ) {
        var ct = xhr.getResponseHeader("content-type"),
            xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
            json = type == "json" || !type && ct && ct.indexOf("application/
json") >= 0,
            data = xml ? xhr.responseXML : xhr.responseText;
        if ( xml && data.documentElement.tagName == "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 ( type == "script" )
            jQuery.globalEval( data );
        // Get the JavaScript object, if JSON is used.
        if ( json )
            data = eval("(" + data + ")");
        return data;
    },
[/code]
PS: sorry for my not-so-perfect english