Bug Tracker

Ticket #11376 (closed bug: worksforme)

Opened 3 years ago

Last modified 2 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by addyosmani

  • Keywords needsdocs added
  • Owner set to larsholm
  • Status changed from new to pending
  • Component changed from unfiled to ajax
  • Priority changed from undecided to low

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.

Last edited 3 years ago by addyosmani (previous) (diff)

comment:2 Changed 3 years ago by addyosmani

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 3 years ago by jaubourg

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 3 years ago by addyosmani

@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 3 years ago by addyosmani

  • Owner changed from larsholm to addyosmani
  • Status changed from pending to assigned

comment:6 Changed 3 years ago by addyosmani

  • Status changed from assigned to closed
  • Resolution set to worksforme

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.

Last edited 3 years ago by addyosmani (previous) (diff)

comment:7 Changed 3 years ago by larsholm

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 3 years ago by jaubourg

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 2 years ago by mikesherov

  • Keywords needsdocs removed
Note: See TracTickets for help on using tickets.