Bug Tracker

Opened 11 years ago

Closed 10 years ago

#4515 closed enhancement (fixed)

$.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 = windoweval?("(" + 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 = windoweval?("(" + 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.

Change History (4)

comment:1 Changed 11 years ago by SnefIT

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.

comment:2 Changed 11 years ago by dmethvin

Component: unfilledajax

comment:3 Changed 10 years ago by webbiedave

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

comment:4 Changed 10 years ago by dmethvin

Resolution: fixed
Status: newclosed

Fixed via #5718.

Note: See TracTickets for help on using tickets.