#11376 closed bug (worksforme)
jQuery does not respect Content-Type with respect to converters
Reported by: | larsholm | Owned by: | addyosmani |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | ajax | Version: | 1.7.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
It seems not to be possible to hit this converter:
$.ajaxSetup({ converters: { "mycustomtype json": function (result) { //do stuff return newresult; } } });
This should be called if the response has Content-Type 'mycustomtype' and the caller expects json.
Change History (9)
comment:1 Changed 11 years ago by
Component: | unfiled → ajax |
---|---|
Keywords: | needsdocs added |
Owner: | set to larsholm |
Priority: | undecided → low |
Status: | new → pending |
comment:2 Changed 11 years ago by
btw, can you confirm whether or not you've specified your custom data-type as follows for the actual request?
.ajax( url, { dataType: "mycustomtype" });
comment:3 Changed 11 years ago by
You just need to add a correspondance between response Content-Type and actual dataType using the contents option:
$.ajaxSetup({ contents: { mycustomtype: /mycustomtype/ }, converters: { "mycustomtype json": function (result) { //do stuff return newresult; } } });
This sure is not well documented.
The reason you have to use another map for this is because response Content-Types and dataTypes never have strict one-to-one correspondance (hence the regular expression).
comment:4 Changed 11 years ago by
@jaubourg thanks for the input on this!. If you'd like to take a stab at improving the docs on this one, please feel free to otherwise I'll add an example based on the above a little later.
comment:5 Changed 11 years ago by
Owner: | changed from larsholm to addyosmani |
---|---|
Status: | pending → assigned |
comment:6 Changed 11 years ago by
Resolution: | → worksforme |
---|---|
Status: | assigned → closed |
As this works fine with the approach @jaubourg outlined above, we're going to close this ticket. I'll update the docs shortly to reflect what was missing.
comment:7 Changed 11 years ago by
But jaubourg's approach does not work. Here's a running example of a custom datatype called 'deltaencoded' which doesn't fire off the converter from deltaencoded to json: http://larsholmjensen.dk/DatatypeTest The server returns json for the first call the both GetData and GetData2, but on the second call GetData returns a response with Content-Type 'deltaencoded'. And as the caller expects json the converter should get called.
comment:8 Changed 11 years ago by
The xhr only produces "text" and "xml" responses, so you need a "text deltaencoded" pass-through converter:
$.ajaxSetup({ contents: { deltaencoded: /deltaencoded/ }, converters: { "text deltaencoded": true, "deltaencoded json": function (result) { //do stuff return newresult; } } });
Now, ajax knows you can pass from text to deltaencoded then deltaencoded to json. Is this working?
comment:9 Changed 10 years ago by
Keywords: | needsdocs removed |
---|
Thanks for your ticket. I was going to post back a few code samples for how to correctly use converters, but I think it would make more sense for you to provide us with a jsFiddle test case that reproduces what you're trying to do first. I don't personally mind if this is hosted somewhere other than jsFiddle as long as I can see an example.
Regardless of whether this behaviour is broken or not, we should be updating the docs for http://api.jquery.com/extending-ajax/#Converters to be more comprehensive. I personally got lost for a while trying to figure out whether this behaviour was supported or not as all our examples use standard types (xml, json) as the request format.