Skip to main content

Bug Tracker

Side navigation

#11376 closed bug (worksforme)

Opened February 23, 2012 09:34AM UTC

Closed February 23, 2012 02:46PM UTC

Last modified October 15, 2012 08:11PM UTC

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.

Attachments (0)
Change History (9)

Changed February 23, 2012 12:21PM UTC by addyosmani comment:1

_comment0: Thanks for your ticket. I was going to post back a few code samples for how to correctly use convertors, 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.1329999860928379
_comment1: 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.1329999926636545
component: unfiledajax
keywords: → needsdocs
owner: → larsholm
priority: undecidedlow
status: newpending

Thanks for your ticket. Could you provide us with a jsFiddle test case that reproduces what you're trying to do?. I don't personally mind if this is hosted somewhere other than jsFiddle as long as I can see an example (I know it can be hard to test converters with fiddle anyway).

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.

Changed February 23, 2012 12:31PM UTC by addyosmani comment:2

btw, can you confirm whether or not you've specified your custom data-type as follows for the actual request?

.ajax( url, {
  dataType: "mycustomtype"
});

Changed February 23, 2012 01:37PM UTC by jaubourg comment:3

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).

Changed February 23, 2012 02:00PM UTC by addyosmani comment:4

@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.

Changed February 23, 2012 02:19PM UTC by addyosmani comment:5

owner: larsholmaddyosmani
status: pendingassigned

Changed February 23, 2012 02:46PM UTC by addyosmani comment:6

_comment0: Based on the above this is working fine at the moment. I'll update the docs shortly.1330008566686397
resolution: → worksforme
status: assignedclosed

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.

Changed February 23, 2012 08:11PM UTC by larsholm comment:7

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.

Changed February 25, 2012 03:20PM UTC by jaubourg comment:8

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?

Changed October 15, 2012 08:11PM UTC by mikesherov comment:9

keywords: needsdocs