Skip to main content

Bug Tracker

Side navigation

#4515 closed enhancement (fixed)

Opened April 10, 2009 05:33PM UTC

Closed June 15, 2010 02:59AM UTC

$.ajax and content-type detection

Reported by: SnefIT Owned by:
Priority: major Milestone: 1.4
Component: ajax Version: 1.3.2
Keywords: ajax,dataType,content-type Cc:
Blocked by: Blocking:
Description

You have to set the dataType option to get the correct datatype (json, script...).

Normally no problem with that, but I have a situation that the response can either be json or just normal html. It should be nice that $.ajax uses the response content-type when the dataType option is not used.

Imho this could be done in the httpData function.

In the httpData we can find:

....

The filter can actually parse the response

if( typeof data === "string" ){

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 = window["eval"]("(" + data + ")");

}

....

This could easily be changed to:

....

The filter can actually parse the response

if( typeof data === "string" ){

If the type is "script", eval it in global context

if ( type == "script" || ( !type && ct.indexOf("javascript") >=

0 ) )

jQuery.globalEval( data );

Get the JavaScript object, if JSON is used.

if ( type == "json" || ( !type && ct.indexOf("json") >= 0 ) )

data = window["eval"]("(" + data + ")");

}

....

The ct variabele is already the response content-type, so that can be used. This way the correct datatype can be returned when the dataType option is ommitted.

Attachments (0)
Change History (4)

Changed April 10, 2009 05:37PM UTC by SnefIT comment:1

You have to set the dataType option to get the correct datatype (json, script...).

Normally no problem with that, but I have a situation that the response can either be json or just normal html. It should be nice that $.ajax uses the response content-type when the dataType option is not used.

Imho this could be done in the httpData function.

In the httpData we can find:

....
// The filter can actually parse the response 
if( typeof data === "string" ){ 
    // 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 = window["eval"]("(" + data + ")"); 
    } 
....

This could easily be changed to:

....
// The filter can actually parse the response 
if( typeof data === "string" ){ 
    // If the type is "script", eval it in global context 
    if ( type == "script" || ( !type && ct.indexOf("javascript") >= 0 ) ) 
        jQuery.globalEval( data ); 

    // Get the JavaScript object, if JSON is used. 
    if ( type == "json" || ( !type && ct.indexOf("json") >= 0 ) ) 
        data = window["eval"]("(" + data + ")"); 
    } 
....

The ct variabele is already the response content-type, so that can be used. This way the correct datatype can be returned when the dataType option is ommitted.

Changed April 11, 2009 04:08PM UTC by dmethvin comment:2

component: unfilledajax

Changed December 26, 2009 09:25PM UTC by webbiedave comment:3

I think it's a more secure approach to require an explicit request for auto-detection/parsing. Otherwise, it wouldn't be out of the realm of

possibility that scripts could be evaluated simply by leaving dataType

unspecified (which many existing apps do).

I've created a ticket for a new dataType setting "auto" which provides this functionality:

http://dev.jquery.com/ticket/5718

Changed June 15, 2010 02:59AM UTC by dmethvin comment:4

resolution: → fixed
status: newclosed

Fixed via #5718.