Opened 14 years ago
Closed 13 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 14 years ago by
comment:2 Changed 14 years ago by
Component: | unfilled → ajax |
---|
comment:3 Changed 13 years ago by
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:
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:
This could easily be changed to:
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.