Bug Tracker

Ticket #2733 (new feature)

Opened 4 months ago

Last modified 2 weeks ago

jQuery.ajax should auto-parse response based on Content-Type, if dataType is not specified

Reported by: jelly-bean Assigned to: anonymous
Type: feature Priority: major
Milestone: 1.3 Component: ajax
Version: 1.2.3 Keywords:
Cc: jelly-bean Needs: Review

Description

i have a feature request that when jQuery.ajax({ dataType: is not specified, that instead of only determining whether to returnStatus or returnText or whatever--that it actually looks at the server's reply in the Content-Type header, and uses that instead to parse XML (text/xml), JSON (application/json), JavaScript (text/javascript), Plain-Text/error (e.g. anything else), etc.

this is because often times i will change the content-type based on the response--i like to send errors in plain text because they trigger a parser error which will fire the jQuery.ajax.error(), while any type of SGML seems to work best passed in XML wrapped as CDATA because it requires less escaped characters vs. JSON which is best for any type of objects or data response, and of course JavaScript for letting the server generate the .js and have the script execute whatever it receives.

Attachments

patch_httpData.txt (0.6 kB) - added by derencius 3 months ago.
a simple patch. tests are needed

Change History

Changed 4 months ago by flesler

  • owner deleted
  • type changed from enhancement to feature
  • component changed from core to ajax

Changed 3 months ago by derencius

a simple patch. tests are needed

Changed 3 months ago by derencius

I'm using the httpData.txt patch with a rails application and it's working fine with javascript content.

Changed 2 weeks ago by derencius

Packaged my patch as a plugin: http://plugins.jquery.com/project/apcth

Changed 2 weeks ago by flesler

  • cc set to jelly-bean
  • milestone changed from 1.2.4 to 1.3

Note that the code you took from httpData is not up to date. Check the trunk for the latest version.

Instead of rewriting httpData, you could actually use the new feature (for 1.3). The dataFilter setting of Ajax requests.

Your code could subscribe to the following AjaxFilter plugin that is sort of a manager and parse the data inside.

Note: See TracTickets for help on using tickets.